[Tutor] new user question about while loops
ajikoe at gmail.com
Wed Oct 26 10:28:47 CEST 2005
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
sumTail += 1
print sumHead, sumTail
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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor