Exec with loops
Just van Rossum
just at letterror.com
Wed Mar 29 02:31:33 CEST 2000
At 11:13 PM +0000 28-03-2000, scottusenet at ithink.org wrote:
>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.
Add an extra \n at the end of the source string.
More information about the Python-list