为什么这抱怨语法无效?
#! /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
为什么这抱怨语法无效?
#! /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
如果你使用的是 Python 3print
是一个函数。像这样称呼它:print(recip)
。
在 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)!
如果是 Python 3,print
现在是一个函数。正确的语法是
print (recip)