Your own while/else


#1




My code seems to me to work correctly, but I keep getting an "Oops try again..." course error message.
If I lose I get: Did you print the "You win!" message?
If I win I get: Did you print the "You lose." message?

I expect to be able to move onward, - what is my error so that I don't fall foul of it again!


from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
while guesses_left > 0:
    
    guess = int(raw_input("Your guess: "))
    if guess == random_number:
        print "Sorry, you lose!"
        print "The hidden was:", random_number
        break
    guesses_left -= 1
else:
    print "The hidden number is:", random_number
    print "You win!"


#2

the else will run the moment you loose, then you print: You win!. if the user guesses the correct number, it prints You lose!, seems a bit off


#3

Ah - I think I get it.

The exercise asks for a game like the last where a win occurs if the number != the random_number. I was following that format.
Silly me...


#4

problem solved?


#5

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