[Tutor] New programmer, need some help getting started on my first project

Chris Delgado prowlerslim at yahoo.com
Fri May 19 06:35:53 CEST 2006


Bob, 

Thanks for the input. I was trying to stick to the lessons in the chapter whose problems I was working on and so I knew using a for loop might be a bit better, I decided to stick with the stuff from the chapter. I do like the way you simplified using the random function, very clever. I need to start thinking like that. Thanks again, I'll have more questions soon I'm sure!

Chris

Bob Gailer <bgailer at alum.rpi.edu> wrote: 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



		
---------------------------------
Be a chatter box. Enjoy free PC-to-PC calls  with Yahoo! Messenger with Voice.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060518/f00e20b6/attachment.html 


More information about the Tutor mailing list