[Tutor] while/if/elif/else loops
Zameer Manji
zmanji at gmail.com
Tue Nov 1 23:36:11 CET 2005
Ok after looking at everyones replies my program looks like this:
#Coin Toss Game
#Zameer Manji
import random
print "This game will simulate 100 coin tosses and then tell you the
number of head's and tails"
tosses = 0
heads = 0
tails = 0
while tosses<100:
if tosses<100:
coin = random.randrange(2)
tosses +=1
if coin == 0:
heads +=1
print "Heads"
else:
tails +=1
print "Tails"
else:
print "100 tosses have been simulated. Please wait for your results"
print "Out of", tosses, ",", heads, "were heads and", tails, "were tails."
This is the results:
>>> ================================ RESTART
================================
>>>
This game will simulate 100 coin tosses and then tell you the number of
head's and tails
Heads
... (To save space the results have been stripped)
Heads
Out of 100 , 48 were heads and 52 were tails.
Thank you for your help everyone.
More information about the Tutor
mailing list