Assignment in a while?
Duncan Booth
duncan.booth at invalid.invalid
Sun Apr 2 15:20:35 EDT 2006
none wrote:
> If I try to run the above code, I get a SyntaxError indicating that I
> can't do an assignment in the while loop. I found a way around this
> (see the commented out while loop), but it seems hackish. Assignment
> within a while loop seems like a pretty standard thing, so I'm just
> curious what I'm missing.
>
Not much, you cannot assign to a variable in the controlling expression
of a while loop. However, for loops do assign values to variables, so if
the form with the break offends you, try restructuring your code as a for
loop. For example:
for results in iter(sth.fetchone, None):
print results
or in many cases you can just fetch everything in one go:
for results in sth.fetchall():
print results
More information about the Python-list
mailing list