2012-09-26 37 views
1

有誰能夠告訴我這是爲什麼給我在空閒的語法錯誤?「print」附近的SyntaxError?

def printTwice(bruce): 
    print bruce, bruce 

語法錯誤:無效的語法

+0

啊,只需添加一個回報有冒號後,不知道爲什麼這樣做,語法錯誤是第一次布魯斯文中陳述部分 –

+4

檢查您的Python版本。這在Python 3.x中是無效的,因爲'print'只是一個函數。 – 2012-09-26 01:54:32

+0

其3.2.3根據空閒 –

回答

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) 

(在Python 2.x的行爲是不同的,where print was a special statement

+0

top show pst,top show,cheers pal。 –

3

你似乎在試圖打印不正確。

你可以使用一個元組:

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

或者你可以在Python中〜2.7字符串使用格式:

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

或者在Python 3中使用函數:

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