[Tutor] Coin Toss Problems
Colleen Glaeser
songbird42371 at gmail.com
Fri Oct 1 09:49:33 CEST 2010
Dear Tutors,
I have an assignment for class due in about 6 hours and I've been working on
it for about 3 hours already, getting nowhere.
My assignment is as follows:
In this exercise, you will write several functions and use them in different
ways. Follow the specifications; I insist.
First, write a function that simulates flipping a coin. This function needs
no parameters. When called, it should return either “HEADS” or “TAILS”. Those
are strings. That’s *return*, not print.
When that function is working, write another function to exercise it. This
function should have one parameter, a positive number. This function will
call the previous function that number of times, and count the number of
heads. When finished, it should *return* (not print) the number of heads.
Now, write code to test it. I want you to try three times. There are three
well-known experiments that I want you to replicate:
The French naturalist Count Buffon (1707-1788) tossed a coin 4040 times.
Results: 2048 heads, or proportion 2048/4040 = 0.5069 for heads.
Around 1900, the English statistician Karl Pearson tossed a coin 24,000
times. Result: 12,012 heads, a proportion of 0.5005.
While imprisoned by the Germans during World War II, the South African
mathematician John Kerrich tossed a coin 10,000 times. Result: 5067 heads, a
proportion of 0.5067.
For each of these, print out enough to explain the result, not just a
number.
One more thing to do: run the Pearson experiment 24000 times. Report the
number of times that the count of heads exceeds the number of tails.
I have gotten a few steps in with my programming, and have completed this,
which will give me heads / tails for as many times as I want. However, it
just lists either "Heads" or "Tails in a column.
import random
def coinToss():
flip = random.randrange(2)
if flip == 0:
return "Heads"
else:
return "Tails"
def multicoinToss(tosses):
for i in range(tosses):
print (coinToss())
print (multicoinToss(20))
However, no matter how many ways I try to alter my code, I just can't work
in some way or some accumulator pattern to add up my number of heads or
tails!
Can anybody help me figure out how to do it? I've found codes that work
with accumulator coin tosses, but they are based on a preset number of
tosses and have nothing to do with returns, etc. in them.
Gah, I hope somebody here can help save me. :(
--
Colleen Glaeser
songbird42371 at gmail.com
636.357.8519
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101001/889daff5/attachment-0001.html>
More information about the Tutor
mailing list