Why doesn't this break work in a while loop?


#1



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


I set it up so that I know what random number is. And so I type in the answer when asked to, and then my loop keeps on going. It doesn't break.


I expected the loop to break, since my answer is equal to the random number generated, wnich is printed before I am asked for answer.


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:
    print random_number
    guess = raw_input('Make your guess!')
    guesses_left -= 1
    if guess == random_number:
        break
        print 'You win!'
else:
    print 'You lose!'


#2

guess is a string, where as random_number is a integer. Lets see what happens:

if "2" == 2:
    print "string and integer are equal"
else:
    print "string and integer are not equal"

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.