Your own while / else: works but i don't get the green light


It seems to work, but i receive this error code none the less:

"Oops, try again. Did you include an else for the while loop?"

any ideas?

random_number = randint(1, 10)
guesses_left = 2

while guesses_left >= 0:    
    guess = int(raw_input("Your guess: "))
    if guess == random_number:
        print "You win!"
        guesses_left -= 1
        print "you lose, guess again!"


you currently have a if/else, the exercise wants you to create a while/else. You could add another else to let the user now they ran out of guesses?


yes now it works. So if I understand correctly, the system will never abort the loop and until I give him a reason to do it.
Only then will he run the code that's after the WHILE function. right?


a program only does what you code it to do. The loop will run until:
condition is false (user has no guesses left)
break is reached (user guesses the correct number)

while loop, but yes, only then


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