2016-12-16 2 views
0

Oft wenn ich Entscheidungen Strukturen (zusammen mit Raw-Eingabe) zu programmieren und zu verwenden, wird die Antwort, die ich wähle ignoriert und geht auf die erste "if" -Anweisung und zeigt die Ausgabe dafür.Python-Programmierung Entscheidung Strukturen

In der Klasse müssen wir ein Spiel mit Schleifen und Entscheidungsstrukturen erstellen. Wenn ich das Programm ausführe, stolpere ich immer wieder über das Problem, dass das Programm die Ausgabe für die 'if' Anweisung anstatt der Antwort ausgibt, die der Benutzer wählt.

Zum Beispiel;

score=0 
while True: 

    optionOne=raw_input("Please pick one of the options!") 


    if (optionOne=="one" or "One" or "ONE"): 
     print "You have succesfully sneaked out without alerting your parents!" 
     print "Your current score is " + str(score) 
     break 
    elif (optionOne=="two" or "Two" or "TWO"): 
     print "Due to stress from work, your mom does not notice your lies and allows you to leave." 
     print "Your current score is " + str(score) 
     break 
    elif (optionOne=="three" or "Three" or "THREE"): 
     print "Your mom is understanding and allows you go to the party!" 
     score=score+10 
     print "You get 10 additional points for being honest!" 
     print "Your current score is " + str(score) 
     break 

Hier wird, obwohl der Benutzer die zweite Option wählt, die Ausgabe für die erste "if" -Anweisung verwendet. Ich bin verwirrt, welche Syntaxfehler oder Fehler ich mache, damit dies geschieht.

Antwort

0

Der Fehler ist hier: if (optionOne=="one" or "One" or "ONE"):

In Python eine leere Zeichenfolge (oder Sequenz) False betrachtet wird, während eine Kette (oder eine Sequenz mit Werten) True betrachtet wird.

>>> bool('') 
False 

>>> bool('One') 
True 

>>>'two'=='one' or 'One' or "ONE" 
'One' 

Im obigen Vergleich 'two'=='one' ist False aber False or 'One' wird 'One' zurück, die True ist.

es wie folgt implementieren:

score=0 
while True: 

    optionOne=raw_input("Please pick one of the options!") 


    if (optionOne in ["one", "One", "ONE"]): 
     print "You have succesfully sneaked out without alerting your parents!" 
     print "Your current score is " + str(score) 
     break 
    elif (optionOne in ["two", "Two", "TWO"]): 
     print "Due to stress from work, your mom does not notice your lies and allows you to leave." 
     print "Your current score is " + str(score) 
     break 
    elif (optionOne in ["three", "Three", "THREE"]): 
     print "Your mom is understanding and allows you go to the party!" 
     score=score+10 
     print "You get 10 additional points for being honest!" 
     print "Your current score is " + str(score) 
     break 
0

Sie

if optionOne == "one" or optionOne == "One" or optionOne == "ONE": 

oder kürzer zu tun haben - Umwandlung von Text Fall

optionOne = optionOne.lower() 

if optionOne == "one": 
    # ... 
elif optionOne == "two": 
    # ... 

Wenn Sie dann verschiedene Wörter haben zu senken können Sie Verwenden Sie in

optionOne = optionOne.lower() 

if optionOne in ("one", "1"): 
    # ... 
elif optionOne in ("two", "2"): 
    # ... 

BTW: Code

if optionOne=="one" or "One" or "ONE": 

als

if (optionOne == "one") or ("One") or ("ONE") 
behandelt wird

und "One" (und "ONE") wird behandelt, als True so haben Sie

if (optionOne == "one") or True or True: 

das ist immer True