2009-12-07 101 views
2

这是为什么抱怨无效的语法?为什么这是无效的语法?

#! /usr/bin/python 

recipients = [] 
recipients.append('[email protected]') 

for recip in recipients: 
    print recip 

我不断收到:

+0

重复:http://stackoverflow.com/questions/826948/python-syntax-error-on-print – 2009-12-07 20:04:47

回答

11

如果您正在使用Python 3 print是一个函数。像这样称呼:print(recip)

+0

我猜蟒蛇3.0版很多重大改变?似乎raw_input不再支持输入。当初学者使用不兼容的版本时,很难让他们遵循示例。 ( – Chris 2009-12-07 17:43:34

+0

)3.0和3.1文档的“新增功能”部分列出了所有3.x更改。 – 2009-12-07 17:47:01

+0

如果您使用基于2.6的示例进行学习,则应该使用2.6。仍可使用。 – recursive 2009-12-07 18:09:29

3

如果它是Python 3,print现在是一个函数。正确的语法是

print (recip) 
4

在python 3中,print不再是一个语句,而是一个function

Old: print "The answer is", 2*2 
New: print("The answer is", 2*2) 

更多蟒蛇3 print功能:

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! 

Old: print >>sys.stderr, "fatal error" 
New: print("fatal error", file=sys.stderr) 

Old: print (x, y)  # prints repr((x, y)) 
New: print((x, y))  # Not the same as print(x, y)!