syntax error in Pyton shell

Erik Max Francis max at alcyone.com
Tue Sep 3 09:58:57 CEST 2002


lion wrote:

> I'm learning exception handling, I type these code in Python shell:
> 
> >>> try: f=open("nofile")
> ... except IOError: pass
> ... print "The exception is just passed!"
>   File "<stdin>", line 3
>     print "The exception is just passed!"
>         ^
> SyntaxError: invalid syntax
> 
> But these code run without any error as a program(or script). Could
> anyone tell me why?

This is an artifact of entering in code manually in the shell, which
normally would have more cues to go on.  In a script it would no that
these two statements were unrelated; in the interactive interpreter it
can't be sure so is waiting for a blank link to separate them.  You can
tell this when the prompt is ... instead of >>>.

Python 2.2 (#1, Feb  6 2002, 19:31:45) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> try: f = open("nofile")
... except IOError: pass
... # note I hit a blank line here
>>> print "The exception just passed!"
The exception just passed!

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list