[Tutor] while/if/elif/else loops

Pujo Aji ajikoe at gmail.com
Wed Nov 2 08:17:33 CET 2005


Hello,

I see two things can be changed a little bit.
1. redundant: while, if
2. print style at the last statement.

see below:

#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:
coin = random.randrange(2)
tosses +=1
if coin == 0:
heads +=1
print "Heads"
else:
tails +=1
print "Tails"

print "100 tosses have been simulated. Please wait for your results"
print "Out of %s, %s were heads and %s were tails" % (tosses, heads, tails)


cheers,
pujo



On 11/1/05, Zameer Manji <zmanji at gmail.com> wrote:
>
> 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.
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051102/fb25e22b/attachment.htm


More information about the Tutor mailing list