[Tutor] help

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 1 Oct 2001 14:01:46 -0700 (PDT)


On Mon, 1 Oct 2001, RIDDLE BOX wrote:

> I am having trouble trying to get a loop into my program, and I am
> wondering if someone could help me better understand using "while"?
> 
> here is a copy of the code that I am having trouble with if someone
> could explain what I am doing wrong I would appreciate it thanks
> 
>     def second_chance(self):
>             j = second
>             while j = open:
>             read = raw_input("where is the file located?")
>             inp = open(read, "r")
>             for line in inp.readlines():
>                 print line

The "while" loop is sorta similar to the "for" loop in that it allows us
to repeat things over and over.


While the "for" loop iterates over a sequence of things, "while" does
something while a condition is true.

For example:

###
while 1:
   print "I am an infinite loop."
###

will run in an infinite loop because "1" is considered to be a true value
in Python.


Here's another example:

###
print "Counting down"
x = 10
while x >= 0:
    print x
    x = x - 1
###



Let's take a look at your program snippet again:

>     def second_chance(self):
>             j = second
>             while j = open:
>             read = raw_input("where is the file located?")
>             inp = open(read, "r")
>             for line in inp.readlines():
>                 print line

There's some small syntax that can be easily corrected: the body of the
while loop needs to be indented.

However, I have to admit that I'm a little confused about how the program
plans to use the while loop.  Can you explain a little more about what you
want to do?  Maybe then we can see if the while loop is a good thing to
use for this method.


Best of wishes to you.