7. While / else question


#1

import random

print "Lucky Numbers! 3 numbers will be generated."
print "If one of them is a '5', you lose!"

count = 0
while count < 3:
    num = random.randint(1, 6)
    print num
    if num == 5:
        print "Sorry, you lose!"
        break
    count += 1
else:
    print "You win!"

Question is what causes the program to print not one number but three numbers from that range?
At first i though it was that count variable but i think is wrong. Can anyone explain this to me?

PS: Merry Christmas to those who celebrate it.


#2

You have a loop that runs up to 3 times and picks a random number each time..? That's got nothing to do with the else part! Am I missing something?


#3

Oh well,I think that makes sense. Thank you :smiley:


#4

I also had a similar question to yours. Thanks for posting it!

P.S. Thanks for being considerate about non-Christmas celebrators! Cheers!


#5

The "while count < 3:" line makes it loop three times...

-- EDIT --
Yup. That's all. I misread something and wrote based on that. It's all good now. (I need a nap.)