breasking out of a while loop (in an if statement)

Harry George hgg9140 at seanet.com
Tue Feb 13 19:43:17 EST 2001


You don't indicate how "text" is initialized, nor why you are using
"i" to index it and "j" as your loop counter.  Probably something
funny there, because the "index out of range" is either those accesses
or the resulting tempString array access.

As for the loop itself, "j>0" might be safer than "j!=0", because you
might come in with a negative number.

Next, I'd print out the result value to see what it *really* is --
there may be conditions where it doesn't act the way you expect. 

Lee Reilly CS1997 <lreilly at cs.strath.ac.uk> writes:

> Hi, I wonder if anyone could be kind enough to point out what I am doing
> wrong here (please see code below).
> 
> The problem arises in the last 5 lines of code. 'result' is a database
> query result (type string). If it's empty then it has size==2 and if not
> then it's >=3. If it is empty then I have placed a return value after
> it. However, if it is not then I would like it to go back around the
> while loop. The code *looks* okay...
> 
> -=---=-=--=-==-=-=-=-==
> while (j!=0):
>     if (radiobutton=='comma'):
>         tempString = string.split(text[i-1], ",")
>     else:
>         tempString = string.split(text[i-1], "\t")
> 
>     matric = string.rstrip(string.lstrip(tempString[0]))
>     SQL = "select * from STUDENTS where matric= '" + matric + "'"
>     result=db_conn.query(SQL)
>     result = len(result)
>     if (result>=3):
>        return "Student with matric value " + matric + " already exists
> in the database"
>        # break
>     else:
>         j=j-1
> -=---=-=--=-==-=-=-=-==
> 
> => Error Type: IndexError
> => Error Value: list index out of range
> 
> I'm using Python with Zope BTW.
> 
> Can anyone see my problem? Any help would be greatly appreciated ;-)
> 
> - Best regards,
> Lee Reilly

-- 
Harry George
hgg9140 at seanet.com



More information about the Python-list mailing list