对于学校我有作业,但我不知道该怎么做。
我有两个电台,一个是startStation(开始),另一个是eindStation(结束)。首先,我必须检查他们是否在车站列表中。这很好。 然而,现在我必须检查是否在同一个列表中eindStation位于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")
我希望你们能帮助我。提前谢谢!
答案 0 :(得分:2)
您需要首先要求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")
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")
编辑: 那个> =应该是>因为没有人想从a旅行到a:)
答案 1 :(得分:0)
我猜用户也要求 beginStation ,与 eindStation 相同,对吗?
如果是,那么您可以进行第一次检查以检查beginStation。 e.g:
if (eindStation in stations_place) and (beginStation in stations_place):
然后最后如果可能是:
if stations_place[eindStation] >= stations_place[beginStation]:
希望这有帮助。
答案 2 :(得分:0)
我不禁想到,在您的代码中将stations_place
定义为list
而不是dictionary
会更好地用于您的目的。
list
是“有序”的,而dictionary
则不是。在您的情况下,站点是有序的,因为它们永远不会改变位置,因此选择有序的数据结构是有意义的
如果您想扩展代码,它会让生活更轻松
即。
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
作为附注,Ruben是你的一个同学,因为这个问题的变体已经在SO Python trainticket machine,所以要小心,你的老师可以找到这个查询,因为它是我搜索的第一个项目引擎查询“python zaandam station”。