"print"附近的SyntaxError?

Spe*_*ire 1 python syntax python-idle

任何人都可以告诉我为什么这在空闲时给我语法错误?

def printTwice(bruce):
    print bruce, bruce
Run Code Online (Sandbox Code Playgroud)

SyntaxError:语法无效

小智 5

检查正在使用的Python版本; 变量sys.version包含有用的信息.

这在Python 3.x中无效,因为print只是一个普通函数,因此需要括号:

# valid Python 3.x syntax ..
def x(bruce): print(bruce, bruce)
x("chin")

# .. but perhaps "cleaner"
def x(bruce):
    print(bruce, bruce)
Run Code Online (Sandbox Code Playgroud)

(Python 2.x中的行为是不同的,其中print有一个特殊的声明.)