Your own while / else


Um for some reason I keep receiving this error if i guess correctly:
"Oops, try again. Did you allow the user 3 guesses, or did you incorrectly detect a correct guess?"
can someone help me figure it out?
it works but only if i lose.

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 num == random_number:
        print "You win!"
        guesses_left -= 1
    guesses_left <=0
    print "You lose"


If your guesses_left -= 1 is inside an else statement you only take away a guess if they guess incorrectly. Which makes practical sense, but the exercise wants you to remove one for every guess.

Also, you don't need the guesses_left <= 0, because the while section will stop running as soon as it hits 0.


Should I move guesses_left inside of the if statement maybe? if I do, what should i put after the else statement below it?


i meant guesses_left -=1


nevermind i figured it out by adding guesses_left to the if statement and the else statement :grin:


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