Your own while/else, prints guess again before You lose


#1

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
else:
print "Guess again."
guesses_left-=1

else:
print "You lose."

How do I stop it from printing guess again after the third guess and just print you lose?


#2

The simplest way is to include one more conditional in the if statement:

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