[Tutor] Problem with a while loop.

Goldie, Josh josh.goldie at mirant.com
Tue May 17 22:22:58 CEST 2005


import random

head = 0
tail = 0

while (head + tail) < 100:
    coin = random.randrange(2)  <-- move this line inside the loop
    if coin == 0: 



-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of . ,
Sent: Tuesday, May 17, 2005 4:12 PM
To: tutor at python.org
Subject: [Tutor] Problem with a while loop.

Hi,

I'm writing a program that is a program that flips a coin 100 times and
then tells the number of heads and tails.

------------------------------------------------------------------------
-------------------------------------------------------------------

import random

head = 0
tail = 0
coin = random.randrange(2)

while (head + tail) < 100:
    if coin == 0:
        print "head"
        head += 1

    elif coin == 1:
        print "tail"
        tail += 1

    else:
        print "Error"


raw_input("")

------------------------------------------------------------------------
-------------------------------------------------------------------

The problem is the program prints like..
tail
tail
tail
.
.
.

Help me!

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! 
http://search.msn.com/

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list