[Tutor] exec sintax error or bug?

Ian Witham witham.ian at gmail.com
Fri Sep 28 00:17:04 CEST 2007


What version of Python arre you running Paulino?

When I run your second snippet on 2.5 I get an error:

>>> exp = "if i == 3 : continue"
>>> for i in range(5):
    exec(exp)
    print i,

Traceback (most recent call last):
  File "<pyshell#17>", line 2, in <module>
    exec(exp)
  File "<string>", line 1
SyntaxError: 'continue' not properly in loop (<string>, line 1)

On 9/28/07, Kent Johnson <kent37 at tds.net> wrote:
>
> paulino1 at sapo.pt wrote:
> > 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 think the code you pass to exec must be syntactically correct on its
> own. It is parsed as if it were in a file by itself, then executed in
> the local scope.
>
> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070928/1b177358/attachment.htm 


More information about the Tutor mailing list