[Tutor] (no subject)

Pierre Dagenais pierre.dagenais at ncf.ca
Wed Oct 1 07:20:53 CEST 2008


kayla bishop wrote:
> I can't figure out how to write a program where you flip a coin 100 times and it keeps track of how many heads and tails you flipped but it has to be random. Can you please help
> _________________________________________________________________
> Get more out of the Web. Learn 10 hidden secrets of Windows Live.
> http://windowslive.com/connect/post/jamiethomson.spaces.live.com-Blog-cns!550F681DAD532637!5295.entry?ocid=TXT_TAGLM_WL_domore_092008
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com 
> Version: 8.0.173 / Virus Database: 270.7.5/1698 - Release Date: 29/09/2008 7:25 PM
>
>   
Here is how one newbie would do it:


import random

# Initialize variables
head = 0
tail = 0

# Flip the coin a hundred times
for x in range(100):
    choice = random.randint(1,2)
   
# Is it 'head'?
    if choice == 1 :
        head = head + 1
       
# If not 'head' then it must be tail
    else :
        tail = tail + 1
       
print "head = ",head
print "tail = ",tail


   


More information about the Tutor mailing list