我对下一个代码有问题。
try: 1+1
except Exception as exception: pass
1+1
try: 2+2
except Exception as exception: pass
我在提示中得到的结果是
... ... File "<stdin>", line 3
1+1
^
SyntaxError: invalid syntax
>>> ... ... ... 4
但是,下一个代码执行时没有出现错误。
try: 1+1
except Exception as exception: pass
try: 2+2
except Exception as exception: pass
我的sys.version_info
是:
sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
为什么会出现语法错误?
使用交互式提示时,块(如try
/except
块)和下一个独立命令之间需要有一条空行。这只是在REPL中,当运行.py
文件时没有必要。