[Tutor] Loop help (fwd)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 7 Apr 2002 16:11:37 -0700 (PDT)


Hi Jeff,

I'm forwarding your question to the main Tutor list at "tutor@python.org".
(You had sent it to tutor-admin, but that only reaches the list
administrators here.)


About your question, let's take a look at the code:

###
while x <= 10:
    x = x + 1
    r=read_number()
    z=x*y
    if z==r:
        print "That's right --well done"
    else:
        print "No, I'm afraid the answer is"
###


Hmmm... actually, I'd like a for loop here:

###
for x in range(1, 11):
    r=read_number()
    z=x*y
    if z==r:
        print "That's right --well done"
    else:
        print "No, I'm afraid the answer is"
###

To make sure that this has the same effect as the previous code, we need
to start the range from 1 instead of zero.


Hope this helps!


---------- Forwarded message ----------
Date: Sun, 7 Apr 2002 15:31:11 -0400
From: Jeff Davis <knowhere@fuse.net>
To: tutor-admin@python.org
Subject: Re: [Tutor] Loop help

On 7 Apr 2002 at 13:15, Cameron Stoner wrote:

> Look down in your code to where I have added code to yours.  It will
> be a while loop.

Why is a "while" loop more appropriate than a "for" loop in this
case?