Reputation: 1
For school I have an assignment to make, but I don't know what to do.
I have two stations, a beginStation (start) and eindStation (end). First I had to check whether they are or are not in a list of stations. This went fine. Now however I have to check if in that same list the eindStation comes after the beginStation.
stations_place = {"Schagen" : 1, "Heerhugowaard" : 2, "Alkmaar" : 3, "Castricum" : 4, "Zaandam" : 5, "Amsterdam Sloterdijk" : 6, "Amsterdam Centraal" : 7, "Amsterdam Amstel" : 8, "Utrecht Centraal" : 9, "'s-Hertogenbosch" : 10, "Eindhoven" : 11, "Weert" : 12, "Roermond" : 13, "Sittard" : 14, "Maastricht" : 15}
eindStation = str(input("What is your end station? "))
if eindStation in stations_place:
print("good") #just to check if the code does it's job here
else :
print("This station isn't available, endstation is: Maastricht")
if eindStation >= beginStation in stations_place.values:
print("good") #just to check if the code does it's job here
else:
print("This station isn't available, endstation is: Maastricht")
I hope you guys can help me out. Thanks in advance!
Upvotes: 0
Views: 141
Reputation: 22433
I can't help thinking that defining stations_place
in your code as a list
rather than a dictionary
would be better for your purposes.
A list
is "ordered", whilst a dictionary
is not. In your case the stations are ordered, in that they never change position, so selecting an ordered data structure makes sense.
It makes life easier, should you want to extend your code.
i.e.
stations_place = ["Schagen","Heerhugowaard","Alkmaar","Castricum","Zaandam","Amsterdam Sloterdijk", "Amsterdam Centraal","Amsterdam Amstel","Utrecht Centraal","'s-Hertogenbosch","Eindhoven","Weert","Roermond","Sittard","Maastricht"]
result = False
while result == False:#Keep asking until a valid start station is input
fromText =""
for i in stations_place:#Build text station list
fromText += i+" > "
print (fromText+"\n")
beginStation = str(input("What is your Starting station? "))
if beginStation in stations_place:
result = True
else:
print("This station isn't available, Starting station is:",stations_place[0],"\n")#first list item
result = False
while result == False:#Keep asking until a valid end station is input
fromS = stations_place.index(beginStation)# Get index of start station
fromText =""
for i in stations_place[fromS:]:#Build text list of following stations
fromText += i+" > "
print (fromText+"\n")
eindStation = str(input("What is your end station? "))
if eindStation in stations_place:
result = True
else :
print("This station isn't available, End station is:",stations_place[-1]+"\n")#Last list item
if stations_place.index(eindStation) > stations_place.index(beginStation):#Check index values
print("Your journey is valid")
elif stations_place.index(eindStation) == stations_place.index(beginStation):#Check index values
print("Your Start and End stations are the same")
else:
print("Your end station is before the start station")
print("Use the other platform for the other direction")
Schagen > Heerhugowaard > Alkmaar > Castricum > Zaandam > Amsterdam Sloterdijk > Amsterdam Centraal > Amsterdam Amstel > Utrecht Centraal > 's-Hertogenbosch > Eindhoven > Weert > Roermond > Sittard > Maastricht >
What is your Starting station? Alkmaar
Alkmaar > Castricum > Zaandam > Amsterdam Sloterdijk > Amsterdam Centraal > Amsterdam Amstel > Utrecht Centraal > 's-Hertogenbosch > Eindhoven > Weert > Roermond > Sittard > Maastricht >
What is your end station? Zaandam
Your journey is valid
As a side note, is Ruben one of your classmates because a variant of this question is already on SO Python trainticket machine, so beware, your teacher could find this query, as it is the first item out of my search engine with a query "python zaandam station".
Upvotes: 0
Reputation: 22433
You need to ask for the beginStation
to begin with.
Here is one way:
stations_place = {"Schagen" : 1, "Heerhugowaard" : 2, "Alkmaar" : 3, "Castricum" : 4, "Zaandam" : 5, "Amsterdam Sloterdijk" : 6, "Amsterdam Centraal" : 7, "Amsterdam Amstel" : 8, "Utrecht Centraal" : 9, "'s-Hertogenbosch" : 10, "Eindhoven" : 11, "Weert" : 12, "Roermond" : 13, "Sittard" : 14, "Maastricht" : 15}
eindStation = str(input("What is your end station? "))
if eindStation in stations_place:
print("good") #just to check if the code does it's job here
else :
print("This station isn't available, endstation is: Maastricht")
beginStation = str(input("What is your Starting station? "))
if stations_place[eindStation] >= stations_place[beginStation]:
print("good") #just to check if the code does it's job here
else:
print("This station isn't available, endstation is: Maastricht")
Edit: That >= should really be > as no one wants to travel from a to a :)
Upvotes: 2
Reputation: 3930
I guess beginStation is also requested from the user , same as eindStation , right?
If yes then you could make the first check to check for beginStation as well. e.g.:
if (eindStation in stations_place) and (beginStation in stations_place):
And then last if could be:
if stations_place[eindStation] >= stations_place[beginStation]:
Hope this helps.
Upvotes: 0