“SyntaxError: Missing parentheses in call to 'print'"是什么意思?在 Python 中是什么意思? [英] What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?

查看:93
本文介绍了“SyntaxError: Missing parentheses in call to 'print'"是什么意思?在 Python 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>>>>打印你好,世界!"文件<stdin>",第 1 行打印你好,世界!"^语法错误:调用打印"时缺少括号

这是什么意思?

解决方案

此错误消息表示您正在尝试使用 Python 3 来遵循示例或运行使用 Python 2 print 的程序声明:

打印你好,世界!"

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

print("Hello, World!")

<小时>

SyntaxError: Missing parentheses in call to 'print'" 是在 Python 3.4.2 中添加的新错误消息,主要是为了帮助在运行时尝试遵循 Python 2 教程的用户Python 3.

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

<预><代码>>>>打印(你好,世界!")你好,世界!

在 Python 3 的早期版本中,解释器只报告一个通用的语法错误,而没有提供任何关于可能出错的有用提示:

<预><代码>>>>打印你好,世界!"文件<stdin>",第 1 行打印你好,世界!"^语法错误:无效语法

至于为什么 print 在 Python 3 中成为一个普通的函数,这与语句的基本形式无关,而是与你如何做的更复杂诸如将多个项目打印到带有尾随空格而不是行尾的 stderr.

在 Python 2 中:

<预><代码>>>>导入系统>>>打印 >>sys.stderr, 1, 2, 3,;打印 >>sys.stderr, 4, 5, 61 2 3 4 5 6

在 Python 3 中:

<预><代码>>>>导入系统>>>打印(1, 2, 3, file=sys.stderr, end="");打印(4、5、6,文件=sys.stderr)1 2 3 4 5 6

<小时>

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

<预><代码>>>>打印你好!"文件<stdin>",第 1 行打印你好!"^语法错误:调用打印"时缺少括号.你的意思是打印(你好!")?

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

为右移运算符引发的 TypeError 也已定制:

<预><代码>>>>打印 >>系统文件回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:>> 不支持的操作数类型:'builtin_function_or_method' 和 '_io.TextIOWrapper'.你的意思是打印(<消息>,文件=<输出流>)"?

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

When I try to use a print statement in Python, it gives me this error:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: Missing parentheses in call to 'print'

What does that mean?

解决方案

This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement:

print "Hello, World!"

The statement above does not work in Python 3. In Python 3 you need to add parentheses around the value to be printed:

print("Hello, World!")


"SyntaxError: Missing parentheses in call to 'print'" is a new error message that was added in Python 3.4.2 primarily to help users that are trying to follow a Python 2 tutorial while running Python 3.

In Python 3, printing values changed from being a distinct statement to being an ordinary function call, so it now needs parentheses:

>>> print("Hello, World!")
Hello, World!

In earlier versions of Python 3, the interpreter just reports a generic syntax error, without providing any useful hints as to what might be going wrong:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: invalid syntax

As for why print became an ordinary function in Python 3, that didn't relate to the basic form of the statement, but rather to how you did more complicated things like printing multiple items to stderr with a trailing space rather than ending the line.

In Python 2:

>>> import sys
>>> print >> sys.stderr, 1, 2, 3,; print >> sys.stderr, 4, 5, 6
1 2 3 4 5 6

In 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


Starting with the Python 3.6.3 release in September 2017, some error messages related to the Python 2.x print syntax have been updated to recommend their Python 3.x counterparts:

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

Since the "Missing parentheses in call to print" case is a compile time syntax error and hence has access to the raw source code, it's able to include the full text on the rest of the line in the suggested replacement. However, it doesn't currently try to work out the appropriate quotes to place around that expression (that's not impossible, just sufficiently complicated that it hasn't been done).

The TypeError raised for the right shift operator has also been customised:

>>> 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>)"?

Since this error is raised when the code runs, rather than when it is compiled, it doesn't have access to the raw source code, and hence uses meta-variables (<message> and <output_stream>) in the suggested replacement expression instead of whatever the user actually typed. Unlike the syntax error case, it's straightforward to place quotes around the Python expression in the custom right shift error message.

这篇关于“SyntaxError: Missing parentheses in call to 'print'"是什么意思?在 Python 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆