8. Your own while / else


#1

from random import randint
random_number = randint(1, 10)
guesses_left = 3
print random_number
while guesses_left != 0:
guess = int(raw_input("Your guess: "))
if guess != random_number:
guesses_left -= 1
print 'Nope, please try again! guesses left: ',guesses_left
elif guess == random_number:
print 'You win! Thanks for playing.'
break
else:
print 'Sorry, you lose!'
break

I'm getting the error message:
Oops, try again.
Did you include an else for the while loop?

If I don't indent the else statement I get and indentation error, and if I only indent it once then it keeps saying my break command is outside the loop.
Everything works fine until you lose the game, then it doesn't seem to run through my else statement.

I'm really not seeing what I'm doing wrong here. Any help will be appreciated.

Thanks,


#2

Might wanna take care that indentation shows in your post when you are asking about indentation.

break only makes sense inside a loop, so you'll have to consider when you want to break out of the loop and where that is in the code, guessing different levels of indentation is not the way to go.

The error message you've got from codecademy complains about you not having a while-else right? Well, do you? What is the difference between while-else and if-else? (I believe you'll want to read over the instructions of the exercise again)