If they give the same answer, why is my code considered incorrect?


#1

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

here's my code:

while guesses_left > 0:
    guess = int(raw_input("your guess: "))
    if random_number == guess:
        print 'You win!'
        break
    else: 
        print "You lose."
    guesses_left -= 1


It runs fine, it asks for three guesses and responds telling me if its true or false, but the program wants me to to put the else function outside of the while loop.Why does it ask that? i mean it does what it asks for, so why does it need to be outside the while loop?

is there something i don't understand?


#2

The else statement belongs outside because you lose if you don't guess the number in the requested number of times, which should be listed as 3, but is not, in your code. (Make sure you fix that.) So if guesses_left is less than or equal to 0, the user has lost, so the lose condition belongs outside your loop.


#3

Other than technical reasons, the section is teaching how the while/else and break function operates vs. an if/else statement.


#4

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