假设列表Ist_bus=['绿茵路','长江路','庐山南‘,闽江路','珠江路','锦江路','香江路','凛江路','黄河路'],存放了某公交线路站名,试编写python程序,根据用户输入起点和终点,计算需要途径站点数并输出。如果输入错误,则提示。
时间: 2024-01-22 09:02:06 浏览: 30
您好!根据您提供的问题,这是一个关于编程的问题,我可以为您提供答案。
以下是解决问题的Python代码:
Ist_bus = ['绿茵路', '长江路', '庐山南', '闽江路', '珠江路', '锦江路', '香江路', '凛江路', '黄河路']
start = input("请输入起点站名: ")
end = input("请输入终点站名: ")
if start not in Ist_bus or end not in Ist_bus:
print("输入的站名有误,请重新输入。")
else:
start_index = Ist_bus.index(start)
end_index = Ist_bus.index(end)
if start_index > end_index:
start_index, end_index = end_index, start_index
stations = Ist_bus[start_index:end_index+1]
print("需要经过的站点数为:", len(stations)-1)
当用户输入起点和终点站名后,程序会判断输入的站名是否在Ist_bus列表中,如果不在则提示输入有误。如果在列表中,则程序会计算需要经过的站点数并输出结果。
相关问题
假设有三个列表: 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]`