[英]Printing list of trainstations going through
所以我得到了一个火车站列表
电台= [“阿姆斯特丹”,“鹿特丹”,“埃因霍温”,“乌得勒支”,“兹沃勒”,“格罗宁根”]
我得到以下def函数:
def test():
beginstation = input('Enter beginstation: ')
laststation = input('Enter laststation: ')
print('You\'re traveling from {} to {}'.format(beginstation, laststation))
print('You are going through the following stations: {}'.format(???))
如果我从阿姆斯特丹乘火车去兹沃勒,我该如何打印我经过的车站? (鹿特丹,埃因霍温和乌得勒支)。
我是编程的新手,正在寻求帮助:)它不是家庭作业! 我只是想变得越来越好。
先感谢您
您可以使用切片,查看此代码,然后将Rotterdam和Zwolle替换为您的输入。
if stations.index('Rotterdam') < stations.index('Zwolle'):
visited = stations[stations.index('Rotterdam'):stations.index('Zwolle')+1]
else:
visited = stations[stations.index('Zwolle'):stations.index('Rotterdam')-1:-1]
print('You are going through the following stations: {}'.format(', '.join(visited)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.