为什么这是无效的语法?
为什么这个会报语法错误呢?
#! /usr/bin/python
recipients = []
recipients.append('me@example.com')
for recip in recipients:
print recip
我一直收到这个错误:
File "send_test_email.py", line 31
print recip
^
SyntaxError: invalid syntax
4 个回答
3
如果你使用的是Python 3,print
现在是一个函数。正确的写法应该是
print (recip)
4
在Python 3中,打印东西不再是一个简单的命令,而是变成了一个函数。
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)!
11
如果你在用Python 3,print
是一个函数。你可以这样使用它:print(recip)
。