【发布时间】:2018-01-17 09:56:51
【问题描述】:
为什么在下面的程序中是 IndentationError 而不是 SyntaxError?
>>> if True:
... print "just right!"
File "<stdin>", line 2
print "just right!"
^
IndentationError: Missing parentheses in call to 'print'
为了确保 IDLE 不只是表现得有趣,我还通过从普通源文件运行这段代码来测试它。仍在引发相同的异常类型。我用来测试的 Python 版本是 Python 3.5.2 和 Python 3.6.1。
据我了解,使用print 时缺少括号被认为是SyntaxError,而不是IndentationError。 What does “SyntaxError: Missing parentheses in call to 'print'” mean in Python? 帖子中的最佳答案似乎也支持这一点:
“SyntaxError: Missing parentheses in call to 'print'” 是 Python 3.4.2 中添加的新错误消息,主要是为了帮助尝试 在运行 Python 3 时遵循 Python 2 教程。
这是一个错误吗?如果有,是什么原因造成的?
【问题讨论】:
标签: python python-3.x syntax-error indentation