14.Your own while / else


#1



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


I printed the random number before guessing and got stuck :confused: My code always makes me enter 3 numbers and i lost every time even when i'm right. Please help i can't find something wrong :confused:


from random import randint

random_number = randint(1, 10)
print random_number
guesses_left = 3

while guesses_left > 0:
    guess=raw_input("Your guess: ")
    if  guess == random_number:
        print "Congratulations! You win!"
        break
    else:
        guesses_left = guesses_left - 1
else:
    print "You lose!"


#2

Hi, @smertunay,

See the documentation for the raw_input function. That function ...

... reads a line from input, converts it to a string ...

You are assigning that string to guess, and then comparing it to an int.

See the documentation for the int function, and use it to convert the user's input to an int prior to the comparison. You could convert it in the same line in which you assign it to guess.


#3

Thank you so much!

if int(guess) == random_number:
print "Congratulations! You win!"
break


#4

The conversion of the user's input to an int can also be performed this way ...

guess = int(raw_input("Your guess: "))

In that case, the test would become ...

if guess == random_number:

#5

That's better. Thank you again ! :slight_smile:


#6