14.8 Your own while/else loop
It seems that these instructions are flawed or maybe I'm not following them correctly, please tell me if I didn't.
In this exercise they want me to create a guessing game. Here i followed all the instructions and it is step 5 that makes my code behave weirdly.
Step 5: Use an else: case after your while loop to print You lose.
I naturally put the else case inside of the if part of my code because that made the most sense to me. When I ran my code it all worked perfectly but it still gave me an error message saying I should put the else case inside of the while case. So I did. The only instructions I got was to print 'You lose' when the while statement is false but its never going to be false because the only way that guesses_left go down is when I win and when I win the while case is going to break either way. So this makes it so its going to ask the same question every time until I guess right. Why would this lesson want me to have a guesses_left variable but not include it well in the code? Just call me out if I missed something super obvious or if I'm just stupid.
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 guess == random_number: print 'You win!' break guesses_left -= 1 else: print "You lose."