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
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to