Exec with loops

scottusenet at ithink.org scottusenet at ithink.org
Tue Mar 28 18:13:30 EST 2000


Is it possible to use exec to run code that includes a loop? For example:
a='for b in range(4):\n print b' exec a This fails with a syntax error. It
also fails if I do this: exec 'for b in range(4):' exec ' print b' But this
works: exec 'for b in range(4): print b' But this limits you to simple loops
and prevents nested loops (and if statements). Anybody know how to use exec
to run code containing arbitrarily complex loops? Thanks.


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list