[Tutor] While Loops: Coin Flip Game :p:

Nithya Nisha nithyakarpagam at gmail.com
Tue Nov 16 05:08:47 CET 2010


Hi Tom,

Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.

> import random
>
> # set the coin

> headsCount = 0
> tailsCount = 0
> count = 0
>
> # the loop
> while count < 100:                                                   #If
you declare count = 0. The while loop condition should be  less than
100.Else you will get 101 counts.
>     *coin = random.randrange(2)*
>     if coin == 0:
>         headsCount += 1
>    else:                                                   #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
>         tailsCount += 1
>     count += 1
>
>
> print "The number of heads was", headsCount
> print "The number of tails was", tailsCount
>
> raw_input("\n\nPress the enter key to exit.")

Regards,
Nithya

_____________________________________________________________________________________________________________________________________________________________
*
Your Description*:

On Mon, Nov 15, 2010 at 7:19 AM, Thomas C. Hicks <paradox at pobox.com> wrote:

> On Sun, 14 Nov 2010 17:16:36 -0500
> Dawn Samson <sdawn at live.ca> 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
>
> >From one beginner to another - it looks to me like you set the value of
> coin once then checked it 100 times.  If you want to reset the value of
> coin maybe it (i.e. setting the value of coin, not just calling
> the value of coin) should be in the loop too?
>
> tom
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101116/f93c3fdb/attachment-0001.html>


More information about the Tutor mailing list