[Tutor] new user question about while loops

Pujo Aji ajikoe at gmail.com
Wed Oct 26 10:28:47 CEST 2005


Hello,
 I'm not so sure about your problem, but
probably this code can help....

sumHead, sumTail = 0,0

listHeadTime = []
listTailTime = []

for i in range(100):
time_start = time.clock()
coin = random.randint(0,1) # 0 head, 1 tail
time_stop = time.clock()
time_duration = time_stop - time_start

if coin == 0:
sumHead += 1
listHeadTime.append(time_duration)
else:
sumTail += 1
listTailTime.append(time_duration)

print sumHead, sumTail
print listHeadTime
print listTailTime

 Cheers,
pujo
 On 10/26/05, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
>
>
> > > My goal, create a program that flips a coin 100 times, at the end it
> > > says the number of times it flipped heads and flipped tails.
> >
> > Do you might if we simplify the problem slightly? Say that you're only
> > flipping the coin two times. Can you write code to say how many times
> > it flips heads and tails? Once you get that working, then modify that
> > program to do it for three coins, then four.
>
> Duh: I forgot to add: can you try to do the above without loops? Since
> there's so little coin flipping going on, the code to do this shouldn't be
> too long to write, even without loops.
>
> (... although it will get longer for each additional coin we flip. When
> we want to scale our solution to more coins, then that's where loops can
> come into play.)
>
> _______________________________________________
> 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/20051026/86030612/attachment.htm


More information about the Tutor mailing list