简体   繁体   中英

Why SyntaxError for code after the try/except/finally block in IDLE?

I am novice of Python, now learning using the app SoloLearn

In the in-app code playground I run these codes with no problem: 在此处输入图片说明

But when I run the same code in IDLE, I get an SyntaxError: 在此处输入图片说明

Does anyone have any idea why?

This has nothing to do with IDLE as it is, as others said, the normal behavior of interactive Python, which executes one statement at a time.

>>> try:
...   print(10/0)
... except:
...   print(5)
... finally:
...   print(6)
... print(7)
  File "<stdin>", line 7
    print(7)
        ^
SyntaxError: invalid syntax

If you put the same code in a file and run the same file in 'batch' mode, it should work fine, whether you run the file from a command line or with IDLE or with any other editor or IDE that will run code in files or an editor buffer.

Since Code Playground has a Run button, I strongly suspect that it is presenting you with an editor window, similar to IDLE and its Run Module F5 menu/shortcut option, not an interactive shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM