“语法”是什么:在呼叫到“ print”中缺少括号。在python中的意思?
当我尝试在Python中使用打印
语句时,它给了我这个错误:
>>> print "Hello, World!"
File "<stdin>", line 1
print "Hello, World!"
^
SyntaxError: Missing parentheses in call to 'print'
这是什么意思?
请参阅获得使用关键字参数end end end yend =''的syntaxerror。
参见 python 3打印括号用于解决方法,并确认打印
不能像的一样工作,就像python 3中的语句。
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?
See Getting SyntaxError for print with keyword argument end=' ' for the opposite problem.
See Python 3 print without parenthesis for workarounds, and confirmation that print
cannot be made to work like a statement in Python 3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
错误消息 SyntaxError:当您尝试使用Python 3语法与Python 2 Print语句时,会在“ print” 呼叫中丢失括号。
示例:
>在Python 3中,打印语句被print()函数替换,需要围绕要打印的值括号。
解决方案
在Python 3中,打印语句被打印()函数代替,需要围绕要打印的值括号。
在Python 3的早期版本中,解释器只是报告了通用语法错误,而没有提供任何可能出现的有用提示:
而对于,为什么为什么在Python 3中的功能与语句的基本形式无关,而是与您如何做更复杂的事情,例如用后续空间打印多个项目,而不是结束行。
在Python 2中:
在Python 3中:
从2017年9月的Python 3.6.3发行开始,已经更新了与Python 2.x 2.x打印语法相关的一些错误消息,以推荐其Python 3.x 3.x对应物:
自从“自“自从”呼叫打印“ case是一个编译时间语法错误,因此可以访问原始源代码,它可以在建议的其余部分中包含全文 替代品。但是,目前尚未尝试制定适当的报价来围绕该表达式放置(这不是不可能的,只是足够复杂,以至于尚未完成)。
typeError
为正确的换档运算符筹集的也已被定制:由于代码运行时会增加此错误,而不是在编译时,它无法访问原始源代码,并且因此,在建议的替换表达式中使用meta-variobles(
&lt; message&gt;
and &lt; output_stream&gt; )打字。与语法错误案例不同,在自定义右移动错误消息中围绕Python表达式的引号很简单。The error message SyntaxError: Missing parentheses in call to 'print' occurs when you attempt to use Python 3 syntax with the Python 2 print statement.
Example:
In Python 3, the print statement was replaced with a print() function, requiring parentheses around the value to be printed.
Solution
In Python 3, the print statement was replaced with a print() function, requiring parentheses around the value to be printed.
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:
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:
In Python 3:
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:
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: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.不幸的是,旧的 XKCD漫画不再完全最新。
你
。
Unfortunately, the old xkcd comic isn't completely up to date anymore.
Since Python 3.0 you have to write:
And someone has still to write that
antigravity
library :(语法从Python 2到Python 3发生了变化。
在Python 2中,
将起作用,但在Python 3中使用括号,因为
这是与Scala相同的语法,靠近Java。
There is a change in syntax from Python 2 to Python 3.
In Python 2,
will work but in Python 3, use parentheses as
This is equivalent syntax to Scala and near to Java.
在python 3.x中,
print
现在是函数,而不是语句,如2.x。因此,
打印
由调用作为函数使用,因此需要括号:python 2.x 2.x :打印” “
python 3.x :print(“戒指之王”)
有多个优点
print
一个函数 打印多个值时更灵活(现在是对函数的参数)。特别是,它允许参数splatting :print> print> print> princt 的行为一个名为
打印
的新功能。对于打印
语句,这是不可能的。In Python 3.x,
print
is now a function, rather than a statement as it was in 2.x.Therefore,
print
is used by calling it as a function, and thus parentheses are needed:Python 2.x: print "Lord of the Rings"
Python 3.x: print("Lord of the Rings")
There are multiple advantages to making
print
a function:print
function offers more flexibility when printing multiple values (which are now arguments to the function). In particular, it allows for argument splatting:print
can be replaced by simply writing a new function namedprint
. This would be impossible with theprint
statement.如果您的代码在Python 2和3中都可以使用,则可以通过在程序开始时加载它来实现此目标:
然后,您可以以Python 3方式打印:
如果您想打印某些东西而不创建新行 - 您可以可以这样做:
If your code should work in both Python 2 and 3, you can achieve this by loading this at the beginning of your program:
Then you can print in the Python 3 way:
If you want to print something without creating a new line - you can do this:
在Python 3中,您只能打印为:
但是在Python 2中,括号不需要。
In Python 3, you can only print as:
But in Python 2, the parentheses are not necessary.
我还可以补充说,我了解有关
python2.7
和python3
之间的语法更改的一切,并且我的代码被正确地写成print(“ String”)
甚至print(f“ string”)
...但是经过一段时间的调试,我意识到我的bash脚本正在调用python,例如:
默认情况下使用
python2.7
来调用我的python脚本,这给出了错误。因此,我将bash脚本更改为:当然使用python3来运行修复错误的脚本。
I could also just add that I knew everything about the syntax change between
Python2.7
andPython3
, and my code was correctly written asprint("string")
and evenprint(f"string")
...But after some time of debugging I realized that my bash script was calling python like:
which had the effect of calling my python script by default using
python2.7
which gave the error. So I changed my bash script to:which of course uses python3 to run the script which fixed the error.
print('Hello,World!')
您正在使用Python 3,在打印时需要支架。
print('Hello, World!')
You're using python 3, where you need brackets when printing.
因此,我遇到了这个错误
,这是一个Python软件包错误,其中使用了Python2,您可能正在Python3上运行此错误。
一种解决方案可能是将python2
转换为Python3
打印 print> print(sothings) 在包装文件夹中的每一行中的每一行,这不是一个好主意So I was getting this error
This is a Python package error, in which Python2 has been used and you are probably running this on Python3.
One solution could be to convert Python2
print something
to Python3print(something)
for every line in each file in the package folder, which is not a good idea????. I mean, you can do it but still there are better ways.To perform the same task, there is a package named 2to3 in Python which converts Python2 scripts to Python3 scripts. To install it, execute the ???? command in terminal..
Then change the directory in terminal to the location where the package files are present, in my case - C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp
Now execute the command ????
and voila, all the Python2 files in that directory will be converted to Python3.
Note:- The above commands hold true for other operating systems as well. Only Python package path will vary as per the system.
在此处的直接答案之外,应该注意python 2和3之间的另一个关键区别。 几乎所有主要差异都涉及您何时应该使用任何一个版本。 此博客文章也做得很好目前
python语言。在继续沿Python 3路线继续前,您应该考虑上述文章。您不仅需要更改某些语法,还需要考虑哪些软件包可供您使用(Python 2的优势)以及可以在您的代码中进行的潜在优化(Python 3的优势) 。
Outside of the direct answers here, one should note the other key difference between python 2 and 3. The official python wiki goes into almost all of the major differences and focuses on when you should use either of the versions. This blog post also does a fine job of explaining the current python universe and the somehow unsolved puzzle of moving to python 3.
As far as I can tell, you are beginning to learn the python language. You should consider the aforementioned articles before you continue down the python 3 route. Not only will you have to change some of your syntax, you will also need to think about which packages will be available to you (an advantage of python 2) and potential optimizations that could be made in your code (an advantage of python 3).
打印“文本”
不是在Python中打印文本的方式,因为这行不通print(“ text”)
将在命令行中的屏幕上打印上述文本print "text"
is not the way of printing text in python as this won't workprint("text")
will print said text on your screen in the command line