Python Forum
Syntax Error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Syntax Error (/thread-21913.html)



Syntax Error - megearhart4 - Oct-20-2019

I am trying to set up a "game" using a while loop for my computer class. The code will generate a random integer between 1 and 100 (rnd), and then the user will submit a guess (temp). If temp is less than rnd, the message "Too Low! Guess again" will display. If temp is higher than rnd, the message "Too high! Guess again" will display. If temp = rnd, the program will display a congratulations message and the number of attempts (guesses) the user submitted. I am using an if/elif/else statement in a while loop. I am getting a syntax error under the else statement when I try to set guesses = guesses + 1. I can't figure out what I did wrong!


import random
done = 1
while done == 1:
    rnd = random.randint(1,100)
    guesses = 0
    temp = input("Guess a number between 1 and 100")
    
    if temp == rnd:
        done = 0
    elif temp << rnd:
        print("Too low! Guess again")
        guesses = guesses + 1
    else:
        print("Too high! Guess again"
        guesses = guesses + 1
              
print("Congratulations! You guessed %f times" % guesses)



RE: Syntax Error - Axel_Erfurt - Oct-20-2019

line 14 should be

print("Too high! Guess again")



RE: Syntax Error - ichabod801 - Oct-20-2019

When you get a syntax error and you don't understand it, always check the line before.