[Tutor] While Loops: Coin Flip Game
Greetings, I'm a Python beginner and working my way through Michael Dawson's Python Programming for the Absolute Beginner. I'm stuck in a particular challenge that asks me to write a program that "flips a coin 100 times and then tells you the number of heads and tails." I've been trying to work on this challenge for a while now and can't get it to work (either it has 100 heads or 100 tails). I've been reworking this code many times and currently what I have does not do anything at all at IDLE. Please take a look at my code below: import random # set the coincoin = random.randrange(2)headsCount = 0tailsCount = 0count = 0 # the loopwhile count <= 100:coinif coin == 0:headsCount += 1 if coin == 1:tailsCount += 1count += 1 print "The number of heads was", headsprint "The number of tails was", tails raw_input("\n\nPress the enter key to exit.") Thanks,S. Dawn Samson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While Loops: Coin Flip Game
Thanks everyone! I should be using algorithms for even such programs at my level. The solution to reiterate the coin flip every time in the loop works. Thanks a lot! Dawn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] While Loops: Coin Flip Game :p:
Greetings, As a thread starter, I thought I should write the rewritten code I got that others helped me get to, since this thread is still going on. # Coin Flips# The program flips a coin 100 times and then# tells you the number of heads and tailsimport random print "\a"print "\tWelcome to 'Coin Flipper!'"print "\nI will flip a coin 100 times and then tell you"print "the number of heads and tails!\n" # set the coinheadsCount = 0tailsCount = 0count = 1 while count <= 100:coin = random.randrange(2)if coin == 0: headsCount += 1else:tailsCount += 1count += 1 print "The number of heads was", headsCountprint "The number of tails was", tailsCount raw_input("\n\nPress the enter key to exit.") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor