2016-11-01 5 views
0

Für die Schule muss ich einen Auftrag machen, aber ich weiß nicht, was ich machen soll.Werte in einer Liste vergleichen

Ich habe zwei Stationen, eine BeginStation (Start) und eindStation (Ende). Zuerst musste ich prüfen, ob sie in einer Senderliste stehen oder nicht. Das ging gut. Jetzt muss ich allerdings prüfen, ob in der gleichen Liste die eindStation nach der beginStation kommt.

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

Ich hoffe, Sie können mir helfen. Danke im Voraus!

Antwort

0

Ich denke, beginStation auch vom Benutzer, gleich wie eindStation angefordert wird, nicht wahr?

Wenn ja, dann könnten Sie die erste Prüfung machen, um auch nach beginStation zu suchen. z.B .:

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

Und dann zuletzt wenn sein könnte:

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

Hoffnung, das hilft.

2

Sie müssen um die beginStation bitten, mit zu beginnen.
ist hier ein Weg:

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: Das> = wirklich sein sollte> als niemand von einem reisen will ein :)

0

ich nicht helfen kann, zu denken definieren stations_place in Ihr Code als list eher als ein dictionary wäre besser für Ihre Zwecke.
Ein list ist "bestellt", während ein dictionary nicht ist. In Ihrem Fall sind die Stationen so geordnet, dass sie ihre Position nie ändern, daher ist die Auswahl einer geordneten Datenstruktur sinnvoll.
Es macht das Leben einfacher, sollten Sie Ihren Code erweitern möchten.
dh

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 

Als Randbemerkung, ist Ruben einer Ihrer Mitschüler, weil eine Variante dieser Frage bereits auf SO Python trainticket machine ist, passen Sie also auf, könnte Ihr Lehrer diese Abfrage finden, wie es aus dem ersten Punkt ist meiner Suchmaschine mit einer Abfrage "Python Zaandam Station".

Verwandte Themen