[Tutor] Challenge

Eric Walker sli1que at yahoo.com
Tue Sep 22 14:39:27 CEST 2009


Ali,
I am new at this but here is my interpretation.
1) flip coin 100 times, and print out the number of heads and number of tails.

from random import Random
score = [0,0]# first position will be heads and second position will be tails
count=0#counter to find out how many times we have flipped.
a = Random()#creates random object
while count < 100:
      answer = a.choice((1,2))
      if answer == 1:
           score[0]+=answer
      else:
           score[1]+=1
      count +=1

print("Heads:%s\nTails:%s" % (score[0], score[1]))



>
>From: Ali Sina <seena_blk at yahoo.com>
>To: tutor at python.org
>Sent: Tuesday, September 22, 2009 5:13:32 AM
>Subject: [Tutor] Challenge
>
>
>Hello tutor
>
>I downloaded a guide about learning Python by Michael Dawson which has challenges at the end of each chapter. I'm a novice so I encountered a problem with this challenge:
>
>
>"Write a program that flips a coin 100 times and then tells 
>you the number of heads and tails."
>I wrote this code but I know its wrong. Although it works properly:
>
>flips=0
>h='heads'
>t='tails'
>while True:
>    flips+=1
>    if flips>100:
>        break
>    if True:
>        print(flips,'=',h or t)
>
>But it prints every number with the string 'heads'. I'm really blank at this. It may have something to do with the 'random' module.
>
>Regards
>
> 
>


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090922/f26a0658/attachment-0001.htm>


More information about the Tutor mailing list