2016-11-01 60 views
0

對於學校我有一個任務要完成,但我不知道該怎麼做。將一個列表中的值比較

我有兩個站,一個beginStation(start)和eindStation(end)。首先,我必須檢查他們是否在電臺列表中。這很好。 但是現在我必須檢查在同一個列表中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

我猜beginStation也請求從用戶,相同eindStation,對吧?

如果是,那麼您可以進行第一次檢查以檢查beginStation。例如: -

if (eindStation in stations_place) and (beginStation in stations_place): 

然後最後如果可能是:

if stations_place[eindStation] >= stations_place[beginStation]: 

希望這有助於。

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") 

編輯: 那> =真正應該>因爲沒有人願意從旅行到:)

0

我不禁想,定義stations_place在您的代碼爲list而不是dictionary會更適合您的目的。
A 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 

作爲一個方面說明,是魯你的同學之一,因爲這個問題的一個變種已經在SO Python trainticket machine,所以要小心,你的老師會發現這個查詢,因爲它是第一個項目出來我的搜索引擎與查詢「python zaandam站」。