2016-09-25 3 views
-1
def magic_n(guess2): #Function, have someone guess if number is 7, max 5 tries 
magicn2 == 7 
tries = 0 
while (tries < 5) and (guess2 != magicn2): 
if guess < 7 : 
    tries = tries + 1 
    print("Too Low") 
    guess2 = int(input("Give me another number since the last one was too high : ")) 
    elif guess > 7: 
    tries = tries + 1 
    print("Too high") 
    guess2 = int(input("Give me another number since the l ast one was too low : ") 
    else : 
    print("Good job you got it") 

guess2 = int (Eingang ("Gib mir eine Nummer bitte:") print (magic_n (guess2)ELIF, ELSE Syntaxfehler Python 3.2.1

Entschuldigt, wenn diese Art und Weise zu fragen nicht ist . Recht, zum ersten Mal mit Stapelüberlauf

+0

die Einrückung ist nicht richtig. Ich denke, das ist es ... –

+0

http://stackoverflow.com/questions/7025443/else-elif-statements-not-working-in-python –

+0

Einrückung ist sehr wichtig in Python. Deines ist völlig falsch. Bitte beheben Sie das zuerst, damit die Frage genau mit Ihrem Code übereinstimmt. Stellen Sie außerdem sicher, dass Sie dem gesamten Codeblock eine zusätzliche Einzugsebene hinzufügen, da SO dies als Code erkennt. Es gibt eine Vorschau, die Sie vor dem Senden überprüfen können. – zvone

Antwort

1

Die identation ist nicht richtig, und Sie benötigen eine weitere Klammer vor „else“

So wäre es:

def magic_n(guess2): 
    magicn = 7 
    tries = 0 
    while (tries < 5) and (guess2 != magicn): 
     if guess2 < 7: 
      tries += 1 
      print("Too Low") 
      guess2 = int(input("Give me another number since the last one was too high : ")) 
     elif guess2 > 7: 
      tries += 1 
      print("Too high") 
      guess2 = int(input("Give me another number since the l ast one was too low : ")) 
     else: 
      print("Good job you got it") 
1

Ja Einrückung ....

Wie Sie Python verwenden ... kann ich vorschlagen, dass Sie PYCHARM betrachten, wie Sie solche Fehler in der Zukunft vermeiden können. Ich benutze es viel und es Auto-Formate für Sie.

https://www.jetbrains.com/pycharm/

0

Linie 12:

guess2 = int(input("Give me another number since the l ast one was too low : ")) //bracket missing 

Einzug in python.there richtig sein muss, ist nicht} in Python so Code-Snippet muss verstehen, wo mein Blockstart tut und wo es endet so

if condition: 
    ........ 
    ........ 
elif condition : 
    ........ 
    ........ 
else : 
    ........