首页 文章

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

提问于
浏览
340

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

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

那是什么意思?

6 回答

  • 15

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

    print "Hello, World!" will work but
    

    在Python 3中,使用大括号作为

    print("Hello, World!")
    

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

  • 11

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

    print("STRING")
    

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

  • 1

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

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

    从Python 3.0开始,你必须写:

    print("Hello, World!")
    

    有人还在写那个 antigravity 库:(

  • 540

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

    打印“你好,世界!”

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

    print("Hello, World!")
    

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

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

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

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

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

    至于为什么 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
    

    在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
    

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

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

    为右移操作员募集的 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>)"?
    

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

  • 3

    如果您的代码应该同时适用于Python 2和3,则可以通过在程序开头加载它来实现:

    from __future__ import print_function   # If code has to work in Python 2 and 3!
    

    然后你可以用Python 3方式打印:

    print("python")
    

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

    for number in range(0, 10):
        print(number, end=', ')
    
  • 2

    在这里的直接答案之外,应该注意python 2和3之间的另一个关键区别.official python wiki几乎涉及所有主要差异,并着重于何时应该使用任何一个版本 . This blog post也很好地解释了当前的python宇宙以及转移到python 3的某种未解决的难题 .

    据我所知,你开始学习python语言 . 在继续沿着python 3路线前,你应该考虑上面提到的文章 . 您不仅需要更改一些语法,还需要考虑可以使用哪些软件包(python 2的一个优点)以及可以在代码中进行的潜在优化(python 3的优势) .

相关问题