[Tutor] Coin Toss Problems

Erik H. venefyxatu+python at gmail.com
Fri Oct 1 12:47:42 CEST 2010

On Fri, Oct 01, 2010 at 02:49:33 -0500, Colleen Glaeser wrote:
> 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.

Looks good, though I prefer random.choice(["HEADS", "TAILS"]) to an if
structure, just because it's shorter. I'm sure endless debates can be
held about various ways to do this though :-)

> 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.

Your multiCoinToss function prints every coin toss. What it should do,
according to this, is count the heads and return that number.
Toss a coin, check if it's heads and if so count it. End by returning
the counter.

Btw, you might want to raise an error if your function receives a
negative number.

> For each of these, print out enough to explain the result, not just a
> number.

You'll want to calculate the ratio for every one of your head counts. 
        print "Ratio %f" % heads/tosses.0

> 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.

Once again, increment a counter :-)

Hope that gets you on your way!



Erik Heeren
E-mail: venefyxatu at gmail.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20101001/b98c0ef6/attachment-0001.pgp>

More information about the Tutor mailing list