What's wrong with this code?


#1

Here is my code:

from random import randint

random_number = randint(1, 10)

guesses_left = 3

while guessesleft > 0:
    guess = int(rawinput("Guess a # 1-9:"))
    if guess == randomnumber:
        print "You win!"
        break
    else:
        print "Try again:"
        guessesleft -= 1
    else: 
        print "You lose."

Here is the error i'm getting:

File "python", line 16
else:
^
SyntaxError: invalid syntax


#2

You might want to re-indent your second else statement. you want that else statement to run, if the while loop is false. So put it at the same indent level as the while loop


#3

Did you mean to write, raw_input()?

Pertaining to what @stetim94 is saying:

while ...:
    # loop body
    break
else:
    # run if NOT break

#4

That worked, thanks!