syntax error in Pyton shell

Bengt Richter bokr at oz.net
Tue Sep 3 10:23:47 EDT 2002


On Tue, 03 Sep 2002 00:58:57 -0700, Erik Max Francis <max at alcyone.com> wrote:

>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 >>>.
>
Why can't it be sure? IWT the dedent should tell it the previous chunk is complete.
And in any case, why not keep accepting lines until the blank line, once into accepting
more than one?

It is annoying to have to resort (interactively) to:

 >>> if 1:
 ...     for i in range(5):
 ...         print i,
 ...     print '[output desired on same line].'
 ...
 0 1 2 3 4 [output desired on same line].

because this screws up:

 >>> for i in range(5):
 ...     print i,
 ... print '[output desired on same line].'
   File "<string>", line 3
     print '[output desired on same line].'
         ^
 SyntaxError: invalid syntax

Regards,
Bengt Richter



More information about the Python-list mailing list