Your own while/else


#1



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


Oops, try again. Did you allow the user 3 guesses, or did you incorrectly detect a correct guess?


If I lose, the exercise completes correctly, but if I win, It generates the above code.

I suspect that I have the break or count decrement is in the wrong spot.


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('Take a guess!'))
    if guess == guesses_left:
        print('You win!')
        break
    guesses_left -= 1    
else:
    print('You lose')


#2

here:

if guess == guesses_left:

You should check if the user guesses equals the random number


#3

Thank you. Not sure what I was thinking there. I didn't even realize that I was checking the users guess to see if it was equal to the number of guesses left. Crazy

Guess that's why us beginners need lots of practice.


#4

Does it all work now and do you understand why?


#5

yes, because I was checking to see if the users guess was equal to the number of guesses and not the generated random number.

Thank you.


#6

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