[Tutor] while loops

Will Harris mosinu at earthlink.net
Mon Aug 8 19:41:34 CEST 2005


I am working my way through "python programming for the absolute beginner" and one of the challenges is to create a program that will flip a coin 100 times and tell you how many of each it did. Now I have it flipping the coin, but when I try to do this 100 times I end up with it running through 100 times, but always the same result comes back. It will either be 100 heads, or 100 tails. I have tried if statements and while loops and both seem to give me all or nothing. I am just looking for a hint at the direction to look of adjust to get the code below working not really the solution. Thanks in advanced for any tips.

#!/usr/bin/python
import random

coin = random.randrange(2)

count = 0
head_count = 0
tail_count = 0

while (count != 100):
        if coin == 0:
                print "You got Heads!"
                head_count = head_count + 1
                count = count + 1
        else:
                print "You got Tails!"
                tail_count = tail_count + 1
                count = count + 1

print "Out of", count, "you flipped", head_count, "heads and ", tail_count, "tails"



More information about the Tutor mailing list