Your own while / else


Your own while / else

File "python", line 10
if guess == random_number:
SyntaxError: invalid syntax

Line 10 is "if guess == random_number:"
What's wrong with that code? Why the error? This is the same syntax as given in the example.

from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
# Start your game!
while guesses_left != 0:
    guess = int(raw_input("Your guess: ")
    if guess == random_number:
        print "You win!"
    guesses_left -= 1
        print "You lose."


Hi @a246248,

Whenever a SyntaxError is reported at the beginning of a line, check the previous line. Is there a problem here? ...

guess = int(raw_input("Your guess: ")


Thank you. A missing parenthesis.


Is the indentation correct for your else block?

To match the while loop, this line should not be indented at all ...



It wasn't. I fixed that too. Thanks again for your help.


No problem; glad it is working now. :smile: