[Tutor] Coin flip game

Benno Lang transmogribenno at gmail.com
Fri Feb 12 00:16:04 CET 2010


On Fri, Feb 12, 2010 at 7:15 AM, Jones, Lawrence D
<lawrence.jones at imperial.ac.uk> wrote:
> My code is below. But can someone please explain to me why the following
> variable has to be placed where it is for the code to work? I thought it
> would need to go nearer the start of the code i.e. just before heads = 0,
> tails = 0 etc:
>                 coin = random.randrange(2)

If you put this at the start of the code (before the loop), then you
only flip the coin once, and then count that single flip 100 times.
That would work, but wouldn't be a very useful program.

> Also, why does the randrange integer have to be ‘2’? I only discovered this
> worked by complete accident. I tried ‘1’ and ‘0,1’ as my integers but they
> just didn’t work.

See: http://docs.python.org/library/random.html#random.randrange
random.randrange parameters are the same as for range, which you can
learn more about here:
http://docs.python.org/tutorial/controlflow.html#the-range-function

HTH,
benno


More information about the Tutor mailing list