[Tutor] Iterating through tokens from a split command

Karl Pflästerer sigurd at 12move.de
Fri Mar 19 13:44:21 EST 2004


On 19 Mar 2004, Vicki Stanfield <- vicki at stanfield.net wrote:

> I am getting an Index Error as I loop through some data from an input
> file. I don't get the error until several iterations into the data. Here
> is the
> code followed by an example of the input. The index error comes on the
> first if statement inside the while loop.


Can you explain what you want to achieve?  And why you use a class for
something which is just a function?

[...]
>     EndOfData = 0
>     try:
>         line = inputfile.readline()
>         if line =="":
>             EndOfData=1
>         tokens=line.split("|")
>         i=0
>         while not EndOfData:

If you have a line EndOfData is here false and will never change.  So
you loop forever.

[...]
>             i=i+1

You increment every loop cycle `i' which gets used as index variable.
Sooner or later you'll get an index error.

[...]
> if __name__  == "__main__":
>     ConvertFile()

Why a class?


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list