[Tutor] Coin toss program

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Nov 5 13:00:41 EST 2003



On Wed, 29 Oct 2003, LONG, SPENCER wrote:

> "Write a program that flips a coin 100 times and then tells you the
> number of heads and tails."
>
> Can anyone forward to this Newbie the code for how this is accomplished?
> So far I have code that flips the coin once (using a random number
> generator), which results in either a heads or tails.  But again, it's a
> single flip of the coin.

Hi Spencer,

Well, one way to do this is to write one hundred lines of code flips.
*grin* But this is probably not a good way to approach the problem.


Have you read about using loops yet?  Here are two links to tutorials that
talk about loops:

    http://www.freenetpages.co.uk/hp/alan.gauld/tutloops.htm
    http://www.ibiblio.org/obp/thinkCSpy/chap06.htm

There are other tutorials at:

    http://www.python.org/topics/learn/non-prog.html

that should have more examples of loops --- as soon as you get them, you
can solve that coin-flipping program in a couple of lines (instead of one
hundred.)



Good luck!




More information about the Tutor mailing list