[英]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.