Reputation:
Train_stations = [ "Perrache", "Ampere", "Bellecour", "Cordeliers", "Louis", "Massena" ]
I started this :
start_station = input("Where are you now?")
ending_station = input("Where would you like to go,")
final = range(start_station) - range(ending_station)
print(final)
It does not work because apparently I can not user range with this type of value..
Upvotes: 1
Views: 57
Reputation: 4606
If the goal is to get the distance between station indexes
you can simply use .index()
and take the abs()
of the difference.
train_stations = [ "Perrache", "Ampere", "Bellecour", "Cordeliers", "Louis", "Massena" ]
start_station = input("Where are you now: ")
ending_station = input("Where would you like to go: ")
final = abs(train_stations.index(start_station) - train_stations.index(ending_station))
print(final)
# Perrache Bellecour = > 2
Upvotes: 1
Reputation: 51643
How to get the index-difference of things inside a list: use the index()
method :
Train_stations = [ "Perrache", "Ampere", "Bellecour", "Cordeliers", "Louis", "Massena" ]
start_station = ""
ending_station = ""
while start_station not in Train_stations:
print("Possible inputs: ", Train_stations)
start_station = input("Where are you now?")
while ending_station not in Train_stations:
print("Possible inputs: ", Train_stations)
ending_station = input("Where would you like to go?")
idx_start = Train_stations.index(start_station)
idx_end = Train_stations.index(ending_station)
print("The stations are {} stations apart.".format ( abs(idx_start-idx_end)))
Output:
Possible inputs: ['Perrache', 'Ampere', 'Bellecour', 'Cordeliers', 'Louis', 'Massena']
Where are you now?Ampere
Possible inputs: ['Perrache', 'Ampere', 'Bellecour', 'Cordeliers', 'Louis', 'Massena']
Where would you like to go?Perrache
The stations are 1 stations apart.
Upvotes: 2