假设列表Ist_bus=['绿茵路','长江路','庐山南‘,闽江路','珠江路','锦江路','香江路','凛江路','黄河路'],存放了某公交线路站名,试编写python程序,根据用户输入起点和终点,计算需要途径站点数并输出。如果输入错误,则提示。
时间: 2023-10-23 12:20:17 浏览: 68
好的,你的问题是关于Python编程的。
以下是我为你设计的Python程序,可以计算公交线路需要经过的站点数量。请将程序复制并保存为.py格式,并在命令行运行。
```
Ist_bus = ['绿茵路', '长江路', '庐山南', '闽江路', '珠江路', '锦江路', '香江路', '凛江路', '黄河路']
start = input("请输入起点站名:")
destination = input("请输入终点站名:")
if start not in Ist_bus or destination not in Ist_bus:
print("输入有误,请重新输入!")
else:
start_index = Ist_bus.index(start)
destination_index = Ist_bus.index(destination)
if start_index < destination_index:
station_num = destination_index - start_index + 1
else:
station_num = start_index - destination_index + 1
print("需要经过", station_num, "个站点。")
```
希望能对你有所帮助!
相关问题
假设有三个列表: 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]`