Your own while / else


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."

The error I received was
  File "python", line 14
SyntaxError: invalid syntax

May I know why I must use the else: case after the while loop instead of what I have done above?


else always needs to be after if or elif, but here:

guesses_left -= 1

you have a different line of code, so else is no longer directly after if

Also, else needs to be after while, given you only want to let the user know they lost when they ran out of guesses


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.