2

为什么这抱怨语法无效?

#! /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
4

3 回答 3

11

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

于 2009-12-07T17:36:57.987 回答
4

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

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

更多 python 3print功能:

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)!
于 2009-12-07T17:41:03.923 回答
3

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

print (recip)
于 2009-12-07T17:37:00.827 回答