假设列表Ist_bus=['绿茵路','长江路','庐山南‘,闽江路','珠江路','锦江路','香江路','凛江路','黄河路'],存放了某公交线路站名,试编写python程序,根据用户输入起点和终点,计算需要途径站点数并输出。如果输入错误,则提示。
时间: 2024-01-22 16:02:06 浏览: 38
可以直接回答。以下是Python程序:
Ist_bus = ['绿茵路', '长江路', '庐山南', '闽江路', '珠江路', '锦江路', '香江路', '凛江路', '黄河路']
start_station = input("请输入起点站名:")
end_station = input("请输入终点站名:")
if start_station not in Ist_bus or end_station not in Ist_bus:
print("输入的站点不存在,请重新输入!")
else:
start_index = Ist_bus.index(start_station)
end_index = Ist_bus.index(end_station)
if start_index < end_index:
stop_count = end_index - start_index
print("需要经过", stop_count, "个站点")
else:
stop_count = start_index - end_index
print("需要经过", stop_count, "个站点")
相关问题
假设有三个列表: Ist_who=["horse","lamb","deer"] Ist_what=["watch a movie","listen to a story","have dinne
这三个列表分别是Ist_who、Ist_what和Ist_when。它们分别表示了三个不同的方面:Ist_who表示参与者,Ist_what表示活动,Ist_when表示时间。具体的内容如下:
Ist_who=["horse","lamb","deer"]:这个列表包含了三个参与者,分别是马、羊和鹿。
Ist_what=["watch a movie","listen to a story","have dinner"]:这个列表包含了三个活动,分别是看电影、听故事和吃晚餐。
Ist_when=["tomorrow","next week","tonight"]:这个列表包含了三个时间,分别是明天、下周和今晚。
这三个列表可以用来组合成不同的句子,例如:"The horse will watch a movie tomorrow."、"The lamb will listen to a story next week."、"The deer will have dinner tonight."等等。
设有列表Ist_odd=[1,3,5,7,9]和列表Ist_even=[2,4,6,8,10]。试编写python程序,将两个列表合并成一个新的列表,并将新列表按照元素的大小降序排列。
可以使用extend()方法将Ist_even中的元素添加到Ist_odd中,然后使用sorted()函数将新列表进行降序排列。
代码如下:
```
Ist_odd=[1,3,5,7,9]
Ist_even=[2,4,6,8,10]
Ist_odd.extend(Ist_even) # 将Ist_even中的元素添加到Ist_odd中
new_list = sorted(Ist_odd, reverse=True) # 对新列表进行降序排列
print(new_list)
```
输出结果为:`[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]`
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)