"print" 在 Python 中抛出无效语法错误 3
"print" throws an invalid syntax error in Python 3
我是 python 的新手。我一直在研究 Codecademy 的课程。我目前也在使用 Pydev/LiClipse。
在 Codecademy 的第一课中,它希望您将变量 parrot 设置为 "Norwegian Blue"。然后它要你使用 len 字符串方法打印鹦鹉的长度。这很简单,我马上就得到了答案:
parrot = "Norwegian Blue"
print len(parrot)
当我将完全相同的代码放入 LiClipse 时,它返回:
语法错误:语法无效
当我将其更改为时,它在 LiClipse 中工作:
打印(len(鹦鹉))
谁能告诉我为什么这在 codecademy 中有效,但在 LiClipse 中无效,为什么添加括号修复了它?
听起来 Pydev/LiClipse 使用的是 Python 3,而 Codeacademy 使用的是 python 2.x 或其他一些旧版本。 python 从 2.x 更新到 3 时所做的更改之一是 print 现在是一个函数。
Python 2:
print "stuff to be printed"
Python 3:
print("stuff to be printed")
它在 CodeAcademy 中有效,因为他们的解释器是 Python 2.7,您不需要括号,因为 print
是一个语句。在 Python 3.0+ 中,print
需要括号,因为它是一个函数。
有关 Python 2.7 和 3.0+ 之间差异的更多信息,请参见此处:
部分样例与上页打印有差异:
Old: print "The answer is", 2*2
New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
最好了解两者之间的区别,以防您使用遗留系统和大量 vs. 在您的私人环境中工作。在 Python 2.7 及以下版本中,print()
有效;但是,省略 ()
s 在 Python 3.0+ 中不起作用,因此最好养成使用它们进行打印的习惯。
Python 2.7 的生命周期预计在 2020 年结束,因此您有足够的时间。
在 Python 3 中,打印已更改为需要括号。 CodeAcademy 可能正在使用 Python 2,看起来您正在使用 Python 3.
https://docs.python.org/3/whatsnew/3.0.html#print-is-a-function
来自文档
Print Is A Function
The print statement has been replaced with a
print() function, with keyword arguments to replace most of the
special syntax of the old print statement (PEP 3105). Examples:
您必须考虑您正在使用的版本。
在 Python 2 中,您的代码将如下所示:
parrot = "Norwegian Blue"
print len(parrot)
在 Python 3 中,您的代码将如下所示:
parrot = "Norwegian Blue"
print ( len(parrot) )
我是 python 的新手。我一直在研究 Codecademy 的课程。我目前也在使用 Pydev/LiClipse。
在 Codecademy 的第一课中,它希望您将变量 parrot 设置为 "Norwegian Blue"。然后它要你使用 len 字符串方法打印鹦鹉的长度。这很简单,我马上就得到了答案:
parrot = "Norwegian Blue"
print len(parrot)
当我将完全相同的代码放入 LiClipse 时,它返回:
语法错误:语法无效
当我将其更改为时,它在 LiClipse 中工作:
打印(len(鹦鹉))
谁能告诉我为什么这在 codecademy 中有效,但在 LiClipse 中无效,为什么添加括号修复了它?
听起来 Pydev/LiClipse 使用的是 Python 3,而 Codeacademy 使用的是 python 2.x 或其他一些旧版本。 python 从 2.x 更新到 3 时所做的更改之一是 print 现在是一个函数。
Python 2:
print "stuff to be printed"
Python 3:
print("stuff to be printed")
它在 CodeAcademy 中有效,因为他们的解释器是 Python 2.7,您不需要括号,因为 print
是一个语句。在 Python 3.0+ 中,print
需要括号,因为它是一个函数。
有关 Python 2.7 和 3.0+ 之间差异的更多信息,请参见此处:
部分样例与上页打印有差异:
Old: print "The answer is", 2*2
New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
最好了解两者之间的区别,以防您使用遗留系统和大量 vs. 在您的私人环境中工作。在 Python 2.7 及以下版本中,print()
有效;但是,省略 ()
s 在 Python 3.0+ 中不起作用,因此最好养成使用它们进行打印的习惯。
Python 2.7 的生命周期预计在 2020 年结束,因此您有足够的时间。
在 Python 3 中,打印已更改为需要括号。 CodeAcademy 可能正在使用 Python 2,看起来您正在使用 Python 3.
https://docs.python.org/3/whatsnew/3.0.html#print-is-a-function
来自文档
Print Is A Function The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105). Examples:
您必须考虑您正在使用的版本。
在 Python 2 中,您的代码将如下所示:
parrot = "Norwegian Blue"
print len(parrot)
在 Python 3 中,您的代码将如下所示:
parrot = "Norwegian Blue"
print ( len(parrot) )