“印刷品”附近的SyntaxError?

2024-05-20 00:38:46 发布

您现在位置:Python中文网/ 问答频道 /正文

有谁能告诉我为什么在空闲时出现语法错误?在

def printTwice(bruce):
    print bruce, bruce

语法错误:语法无效


Tags: def语法空闲print语法错误bruceprinttwice
2条回答

你好像想打印错误。在

可以使用元组:

def p(bruce):
    print (bruce, bruce) # print((bruce, bruce)) should give a tuple in python 3.x

也可以在Python~2.7中使用字符串格式:

^{pr2}$

或者使用Python 3中的函数:

def p(bruce):
    print("{0}{1}".format(bruce, bruce))

检查正在使用的Python版本;变量^{}包含有用的信息。在

这在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)

(Python2.x中的行为是不同的,where ^{} was a special statement。)

相关问题 更多 >