Question 8: Your own while/else


#1

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

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!")
        break
    else:
        print ("Guess again.")
        guesses_left -= 1
else:
    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.
None


#2

simply check if it was there last guess:

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

#3

Thank you, stetim94!


#4