[Tutor] new user question about while loops
Nick Eberle
NEberle at lucasarts.com
Wed Oct 26 08:04:08 CEST 2005
print "This is a coin flip game"
print "\n100 flips will be made and it will show the amount of times heads or tails came up"
import random
#assigning variables to track the amount of times heads or tails comes up
heads=0
tails=1
flip=100
while flip < 100:
flip -= 1
random.randrange(2)
if flip < 100:
break
print "\nThe amount of times tails came up was" , tails , "The amount of times heads came up was" , heads
raw_input()
________________________________
From: bob [mailto:bgailer at alum.rpi.edu]
Sent: Tue 10/25/2005 10:58 PM
To: Nick Eberle; tutor at python.org
Subject: Re: [Tutor] new user question about while loops
At 10:53 PM 10/25/2005, Nick Eberle wrote:
Content-class: urn:content-classes:message
Content-Type: multipart/alternative;
boundary="----_=_NextPart_001_01C5D9F1.A836CF4F"
Sorry for potential double post, error with first send
----------------------------------------------------------------------
Hello all,
I had a new question about python. I am pretty much in tutorial learning stages, but attempting to create sample programs not in my book to learn how to construct scripts.
I understand the format of while loops, but is it possible to use the random.randrange function in them?
My goal, create a program that flips a coin 100 times, at the end it says the number of times it flipped heads and flipped tails.
My dilemia, how do I get this to work with a while loop? I tried intially assigning
heads=0
tails=1
then I figured I could just do a while loop and then use the random function each time. At the end it could count the number of times the random generated a 0 or a 1.
However I can't seem to construct it in a way that makes or works.
Show us your code, as flawed as it might be. Then we can advise you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051025/5ae2157e/attachment.html
More information about the Tutor
mailing list