[Tutor] Coin Toss Problems

Alan Gauld alan.gauld at btinternet.com
Fri Oct 1 16:15:13 CEST 2010

"Colleen Glaeser" <songbird42371 at gmail.com> 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.

OK, You've done this

> When that function is working, write another function to exercise 
> it.

And you have almost done 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.

But not quite...

> def multicoinToss(tosses):
>     for i in range(tosses):

So far so good

>        print (coinToss())

But instead of printing you want to count heads
Do you know how to test if the result of your function is "HEADS"?
Do you know how to increment a counter - ie add one to it?
Do you know how to return that value from your function at
the end of the for loop?

You should do, because you have done something very
similar to 1 and 3 in  your coinToss function.

> Can anybody help me figure out how to do it?

Because its homework we won;t give you the answer just
some hints. Have another go and post the result if it doesn't

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list