[Tutor] While Loops: Coin Flip Game

Adam Bark adam.jtm30 at gmail.com
Mon Nov 15 00:09:58 CET 2010


On 14/11/10 22:16, Dawn Samson wrote:
> 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 coin
> coin = random.randrange(2)
> headsCount = 0
> tailsCount = 0
> count = 0
>
> # the loop
> while count <= 100:
>     coin
>     if coin == 0:
>         headsCount += 1
>     if coin == 1:
>         tailsCount += 1
>     count += 1
>
> print "The number of heads was", heads
> print "The number of tails was", tails
>
> raw_input("\n\nPress the enter key to exit.")
>
> Thanks,
> S. Dawn Samson
You try to print two variables, "heads" and "tails" which don't exist. 
The other replies covered the other main problems.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101114/be2a3eac/attachment.html>


More information about the Tutor mailing list