Python Forum

Full Version: Syntax Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
line 14 should be

print("Too high! Guess again")
When you get a syntax error and you don't understand it, always check the line before.