Your own while / else


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/0/8?curriculum_id=4f89dab3d788890003000096#


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!"
        break
    else:
        guesses_left -= 1
else:
    guesses_left <=0
    print "You lose"


#2

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.


#3

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


#4

i meant guesses_left -=1


#5

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


#6

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