打印列车的列车

时间:2017-09-29 17:17:21

标签: python

所以我得到了一个列表,例如

station = ['Amsterdam','Rotterdam','Eindhoven','Utrecht','Zwolle','Groningen']

我得到了以下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(???))

如果我从阿姆斯特丹乘坐火车到兹沃勒,我如何打印出我经过的车站? (鹿特丹,埃因霍温和乌得勒支)。

我是编程初学者并寻求帮助:)这不是功课!我只是想变得越来越好。

提前谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用切片,查看此代码,然后将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)))