[Tutor] Challenge

Tim Bowden tim.bowden at mapforge.com.au
Wed Sep 23 06:02:51 CEST 2009


On Tue, 2009-09-22 at 05:13 -0700, Ali Sina wrote:
> Hello tutor
> 
> I downloaded a guide about learning Python by Michael Dawson which has
> challenges at the end of each chapter. I'm a novice so I encountered a
> problem with this challenge:
> 
> 
> 
> "Write a program that flips a coin 100 times and then tells you the
> number of heads and tails."

The heads and tails bit is a red herring.  Don't try and model it in
your code.  It does nothing more than signal a binary condition, and
provide a tag to hang the result on (at least in this context).

#!/usr/bin/python
import random

i=0
for flip in range(100):
  i += random.choice((0,1))

print "Heads: %d, Tails: %d" % (i, 100-i)
#

Tim bowden




More information about the Tutor mailing list