SyntaxError near “print”? [closed]

天涯浪子 提交于 2019-11-27 08:40:47

问题


can anybody please tell me why this is giving me syntax error in idle?

def printTwice(bruce):
    print bruce, bruce

SyntaxError: invalid syntax


回答1:


Check the version of Python being used; the variable sys.version contains useful information.

That is invalid in Python 3.x, because print is just a normal function and thus requires parenthesis:

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

# .. but perhaps "cleaner"
def x(bruce):
    print(bruce, bruce)

(The behavior in Python 2.x is different, where print was a special statement.)




回答2:


You seem to be trying to print incorrectly.

You can use a Tuple:

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

Or you can use formatting in a string in Python ~2.7:

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

Or use a function in Python 3:

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


来源:https://stackoverflow.com/questions/12593502/syntaxerror-near-print

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!