8. Your own while/else


I’ve tried so many different variations here, but I don’t understand what’s wrong with my syntax there. Can anybody suggest a solution? I’m starting to lose my mind


The below code works for me

The point is when the number guessed (guess) matches the random_number, the print statement ‘You Win!’ is executed loop and breaks ,if guess is wrong while loop gets executed till condition is met (guess_left = 3).


I had the same problem… You are missing a closing parenthesis before the if-statement.


why is the “else” statement aligned with the “while” statement rather than with the “if” statement?


# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)
print "All right, you wanna play Guess the Number?"
wanna_play = (raw_input("Wanna play? "))
print "I didn't really hear you, but I don't care."
print "I'm gonna choose one number between 1 and 10. You HAVE to guess."
guesses_left = 3
# Start your game!
while guesses_left >= 1:
    guess = int(raw_input("Your guess: "))
    if guess == random_number:
        print "You guessed right. You win, I guess."
        print "Wrong."
        guesses_left -= 1
    print "Game over. You lose. Ha!"

That worked for me.

int(raw_input("Guess my number please: ") 

Looks like your input code is missing a parentheses at the end.

should be:

int(raw_input("Guess my number please: "))