Question 8: Your own while/else


I'm trying to figure out how to prevent the text "Guess again." from being printed after the third guess entered is incorrect. I want the output to print "You lose." without printing "Guess again." before that happens.

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!")
        print ("Guess again.")
        guesses_left -= 1
    print ("You lose.")

For instance, this is what the output looks like now:

Your guess: 5
Guess again.
Your guess: 3
Guess again.
Your guess: 7
Guess again. This is what I want to not appear.
You lose.


simply check if it was there last guess:

if guesses_left != 1: print ("Guess again.")


Thank you, stetim94!