[Tutor] New programmer, need some help getting started on my first project
Bob Gailer
bgailer at alum.rpi.edu
Fri May 19 04:05:03 CEST 2006
Chris Delgado wrote:
> Bob et al,
>
> Ok guys, program is all fixed up and runs well. HEre is the final
> code. Thanks for the help and its on to the next prog. Thanks for the
> patience and help!
>
> Chris
>
> # A program that simulates flipping a coin 100 times and the reports
> the number of heads and tails that were flipped#
>
> import random
>
> heads = 0
> tails = 0
> flips = 0
>
> while flips < 100:
>
> coin = random.randrange(2)
>
> if coin == 0:
> heads += 1
> else:
> tails += 1
>
> flips += 1
>
> print "The coin was flipped 100 times and it was heads " +str(heads)+
> " times and tails " + str(tails) + " times!"
>
> raw_input("\n\nPress the Enter key to exit")
Congrats! Now consider some "simplifications":
import random
heads = 0
for flips in range(1,101): heads += random.randrange(2)
print "The coin was flipped %s times and it was heads %s times and tails
%s times!" % (flips, heads, flips - heads)
raw_input("\n\nPress the Enter key to exit")
--
Bob Gailer
510-978-4454
More information about the Tutor
mailing list