[Tutor] While Loops: Coin Flip Game

Robert Berman bermanrl at cfl.rr.com
Sun Nov 14 23:47:04 CET 2010


From: tutor-bounces+bermanrl=cfl.rr.com at python.org
[mailto:tutor-bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Dawn Samson
Sent: Sunday, November 14, 2010 5:17 PM
To: tutor at python.org
Subject: [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
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
>>>>>

Hi,

Think in terms of logical steps. You need to generate the results of a random
coin toss for every toss of the coin. Therefore, if you are tossing 100
tosses, you need to generate 100 results based on a probability of zero or
one.

Since this is your homework assignment you write the code, but in terms of a
logical frame work you would have something like the following.
Tossknt = 1
Generate a random toss ; results being either one or 0. Further , assume 0 is
tail; heads is one.
If toss = 1 headcount = headcount + 1
Else tailcount = tailcount + 1
Tossknt = Tossknt + 1
If Tossknt < 100 Continue loop
Else print ‘nbr of heads tossed = ‘, headcount
	Print ‘nbr of tails tossed = ‘, tailcount

Obviously, this is not valid python code but it should give you enough
information to solve your problem.

Good luck,

Robert


--
I am using the free version of SPAMfighter.
We are a community of 7 million users fighting spam.
SPAMfighter has removed 53 of my spam emails to date.
Get the free SPAMfighter here: http://www.spamfighter.com/len

The Professional version does not have this message




More information about the Tutor mailing list