2016-11-01 18 views
0

学校の場合、私は割り当てをしていますが、何をすべきかわかりません。1リスト内の値を比較してください

私は2つのステーション、beginStation(start)とeindStation(end)を持っています。最初に、彼らが駅のリストに入っているかどうかをチェックしなければなりませんでした。これはうまくいった。 しかし、同じリスト内で、eStationStationが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") 

編集: 誰も私がstations_placeを定義することを考えて助けることができないから:)

0

に旅行したいと考えていないよう> =が本当に>でなければなりませんdictionaryではなく、listというコードを使用すると、より効果的です。
listは「注文済」であり、dictionaryは「注文済み」です。あなたの場合、ステーションの位置は決して変わらないので、ステーションは順序付けされているので、順序付けられたデータ構造を選択することは理にかなっています。
あなたのコードを拡張したいなら、それは人生を楽にします。この質問の変形がSO Python trainticket machineにすでにあるので、注意してくださいので、サイドノートとして

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 

すなわち
は、ルーベンは、あなたのクラスメートの一つであり、それが出て最初の項目であるとして、あなたの先生は、このクエリを見つけることができます"python zaandam station"というクエリを使って検索エンジンを検索しました。

関連する問題