[Tutor] new user question about while loops

Nick Eberle NEberle at lucasarts.com
Wed Oct 26 17:23:35 CEST 2005


Ahh makes much more sense, thanks for all the help! 

I'll go back and rework it, keeping in mind trying to solve each piece separately.

Thanks again all.

-----Original Message-----
From: Alan Gauld [mailto:alan.gauld at freenet.co.uk]
Sent: Wednesday, October 26, 2005 1:53 AM
To: Nick Eberle; bob; tutor at python.org
Subject: Re: [Tutor] new user question about while loops


As Danny says, try breaking the problem into chunks and solving
each bit separately. I'll provide a few comments on your code in
the meantime but try the "evolving soluition" approach too.

#assigning variables to track the amount of times heads or tails comes up
heads=0
tails=1

AG> Why not make both zero, after all there have been no flips so far!

flip=100
while flip < 100:

AG> flip is never less than 100 because you set it to 100!
AG> Thus you never enter the loop...

    flip -= 1
    random.randrange(2)
    if flip < 100:
        break

AG> And if you did after the first time through you make flip equal 99
AG> so that it will always exit the loop(break) here so it would only run 
once!

AG> Try getting a while loop to run 100 times printing out the loop test 
value.
AG> Or better stuill, since you know how many times you want the lopp to
AG> run use a for loop instead:

AG> for loop in range(100):
AG>     # loop code here

print "\nThe amount of times tails came up was" , tails , "The amount of 
times heads came up was" , heads

AG> But the only values you assign to head/tails are the initial 1 and 0.
AG> Try Danny;'s suggestion of flipping the coin twice...

 HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list