looping through a list
Emile van Sebille
emile at fenx.com
Fri Apr 26 22:13:08 EDT 2002
Hugh writes:
> result = c.fetchone()
Try this in the interpreter. I'd expect (but don't know for sure) that
you're getting a list back...
> if (result != ""):
If so, you'll always pass through this...
... and if you're getting a string you could be saying ' if result:'
> i=0
> text = '<tr width = "80%" align = "center">'
you may want to be using a list here instead of a string. appending to
lists is more efficient than concatenating strings.
> while (i != len(result)+1):
this could be ' for returneditem in result:' assuming a list result.
> i = i + 1
> text = text + '<td align = "center" valign =
> "top">'+str(result[int(i)]) +'</td>'
you certainly don't need int() here.
> text = text + "</tr>"
this probably wants to be
text.append('''<td align = "center" valign ="top">%s</td></tr>''' %
returneditem)
> page.generate("","",sessionID,text)
> else:
> text = '<h4>No items found</h4>'
> page.generate("","",sessionID,text)
>
>
> At the moment, it keeps saying list index out of bounds and I don't
> know where I'm going wrong. I've tried all sorts of things in the
> While statement and still can't get it going.
You're probably going wrong by adding 1 in testing
while (i != len(result)+1):
as i subsequently indexes into result. In python, indexes end one shy
of the length.
HTH,
--
Emile van Sebille
emile at fenx.com
---------
More information about the Python-list
mailing list