为什么这个语法无效?

时间:2009-12-07 17:35:04

标签: python printing python-3.x syntax-error

为什么抱怨语法无效?

#! /usr/bin/python

recipients = []
recipients.append('chris@elserinteractive.com')

for recip in recipients:
    print recip

我一直在:

File "send_test_email.py", line 31
    print recip
              ^
SyntaxError: invalid syntax

3 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:4)

在python 3中,print不再是声明,而是function

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

更多python 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)!

答案 2 :(得分:3)

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

print (recip)