So I got a list of trainstations for example
stations = ['Amsterdam', 'Rotterdam', 'Eindhoven', 'Utrecht', 'Zwolle', 'Groningen']
I got the following def function:
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(???))
If I take the train from Amsterdam to Zwolle, how do i print out the stations i go through? (Rotterdam, Eindhoven and Utrecht).
I'm a beginner at programming and looking for some help :) Its not homework! I'm just trying to get better and better.
Thank you in advance
You can use slicing, look at this code and then replace the Rotterdam and Zwolle for your inputs.
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)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.