在Python中,"SyntaxError:调用'print'时缺少括号"是什么意思?

nco*_*lan 378 python python-3.x

当我尝试print在Python中使用语句时,它给了我这个错误:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: Missing parentheses in call to 'print'
Run Code Online (Sandbox Code Playgroud)

那是什么意思?

nco*_*lan 583

此错误消息表示您尝试使用Python 3来关注示例或运行使用Python 2 print语句的程序:

print "Hello, World!"
Run Code Online (Sandbox Code Playgroud)

上面的语句在Python 3中不起作用.在Python 3中,您需要在要打印的值周围添加括号:

print("Hello, World!")
Run Code Online (Sandbox Code Playgroud)

"SyntaxError:调用'print'时缺少括号"是Python 3.4.2中添加的新错误消息,主要是为了帮助那些在运行Python 3时尝试遵循Python 2教程的用户.

在Python 3中,打印值从一个独特的语句变为一个普通的函数调用,所以它现在需要括号:

>>> print("Hello, World!")
Hello, World!
Run Code Online (Sandbox Code Playgroud)

在早期版本的Python 3中,解释器只报告一般语法错误,而不提供任何有用的提示,指出可能出现的问题:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

至于为什么 print成为Python 3中的普通函数,它与语句的基本形式无关,而是与你如何做更复杂的事情,比如用尾随空格打印多个项目到stderr而不是结束行.

在Python 2中:

>>> import sys
>>> print >> sys.stderr, 1, 2, 3,; print >> sys.stderr, 4, 5, 6
1 2 3 4 5 6
Run Code Online (Sandbox Code Playgroud)

在Python 3中:

>>> import sys
>>> print(1, 2, 3, file=sys.stderr, end=" "); print(4, 5, 6, file=sys.stderr)
1 2 3 4 5 6
Run Code Online (Sandbox Code Playgroud)

从2017年9月的Python 3.6.3版本开始,一些与Python 2.x打印语法相关的错误消息已经更新,以推荐他们的Python 3.x版本:

>>> print "Hello!"
  File "<stdin>", line 1
    print "Hello!"
                 ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello!")?
Run Code Online (Sandbox Code Playgroud)

由于"调用打印时缺少括号"的情况是编译时语法错误,因此可以访问原始源代码,因此可以在建议替换中的行的其余部分包含全文.但是,它目前没有尝试找出适当的引号来放置该表达式(这不是不可能的,只是足够复杂而没有完成).

TypeError右移操作员的升起也是定制的:

>>> print >> sys.stderr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?
Run Code Online (Sandbox Code Playgroud)

由于在代码运行时引发此错误,而不是在编译时引发错误,因此它无法访问原始源代码,因此在建议的替换表达式中使用元变量(<message><output_stream>)而不是用户实际键入的任何内容.与语法错误情况不同,在自定义右移错误消息中围绕Python表达式放置引号是很简单的.

  • 你好!我认为这个工具可以帮助某人https://docs.python.org/2/library/2to3.html (4认同)
  • 感谢@ antti-haapala在顶部添加了直接回答问题的摘要,然后继续对错误消息的来源进行更长的解释:) (3认同)

Chr*_*ian 21

不幸的是,旧的xkcd漫画不再完全是最新的.

https://imgs.xkcd.com/comics/python.png

从Python 3.0开始,你必须写:

print("Hello, World!")
Run Code Online (Sandbox Code Playgroud)

有人还在写那个antigravity图书馆:(

  • 反重力虽然存在...您是否尝试导入它?;) (2认同)

小智 18

从Python 2到Python 3的语法发生了变化.在Python 2中,

print "Hello, World!" 
Run Code Online (Sandbox Code Playgroud)

将工作,但在Python 3中,使用括号

print("Hello, World!")
Run Code Online (Sandbox Code Playgroud)

这是Scala和Java附近的等效语法.


Om *_*Sao 7

基本上,从 Python 3.x 开始,您需要使用print括号。

Python 2.x:打印“指环王”

Python 3.x:打印(“指环王”)


说明

print是一个声明2.x的,但它是一个功能3.X。现在,有很多很好的理由。

  1. 使用 Python 3.x 的函数格式,在打印多个项目时使用逗号分隔具有更大的灵活性。
  2. 你不能在语句中使用参数。在 3.x 中,如果您有要使用分隔符打印的项目列表,您可以这样做:
>>> items = ['foo', 'bar', 'baz']
>>> print(*items, sep='+') 
foo+bar+baz
Run Code Online (Sandbox Code Playgroud)
  1. 您不能覆盖语句。如果你想改变 print 的行为,你可以在它是一个函数时这样做,而不是在它是一个语句时。


Lar*_*rry 6

在 Python 3 中,您只能打印为:

print("STRING")
Run Code Online (Sandbox Code Playgroud)

但在 Python 2 中,括号不是必需的。


Luk*_*asz 5

如果您的代码可以在 Python 2 和 3 中运行,您可以通过在程序开头加载它来实现这一点:

from __future__ import print_function   # If code has to work in Python 2 and 3!
Run Code Online (Sandbox Code Playgroud)

然后就可以用Python 3的方式打印了:

print("python")
Run Code Online (Sandbox Code Playgroud)

如果您想在不创建新行的情况下打印某些内容 - 您可以这样做:

for number in range(0, 10):
    print(number, end=', ')
Run Code Online (Sandbox Code Playgroud)


Alf*_*avo 5

Python2.7我还可以补充一点,我知道关于和之间语法变化的一切Python3,并且我的代码正确地写为print("string")and 甚至 print(f"string")......

但经过一段时间的调试后,我意识到我的 bash 脚本正在调用 python,如下所示:

python 文件名.py

默认情况下,它会调用我的 python 脚本,python2.7从而产生错误。所以我将 bash 脚本更改为:

python3 文件名.py

哪个粗略使用 python3 来运行修复错误的脚本。