Your own while / else


#1



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!"
        break
    guesses_left -= 1
    else:
        print "You lose."


#2

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: ")

#3

Thank you. A missing parenthesis.


#4

Is the indentation correct for your else block?

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

else:

#5

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


#6

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


#7