[Tutor] exec sintax error or bug?

paulino1 at sapo.pt paulino1 at sapo.pt
Thu Sep 27 18:48:14 CEST 2007


Hello!

Why doesn't the second code snipet, work like the first?

>>> for i in range(5):
...    if i == 3 : continue
...    print i,
...
0 1 2 4

>>> exp = "if i == 3 : continue"
>>> for i in range(5):
...    exec( exp )
...    print i,
...
Traceback (most recent call last):
   File "<input>", line 2, in <module>
   File "<string>", line 1
SyntaxError: 'continue' not properly in loop (<string>, line 1)


I have other examples os exp = "if <some condition> : do this" that  
work as expected when called by exec(exp)

So i think the problem is not about "continue" but about "exec".

>>> exp2 = 'if i == 2 : print 2**2'
>>> for i in range(5):
...    exec( exp2 )
...    print i,
...
0 1 4
2 3 4


More information about the Tutor mailing list