Вопрос

Почему это жалуется на неверный синтаксис?

#! /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 3 print это функция.Назовите это так: print(recip).

Другие советы

В Python 3 print больше не является оператором, а функция.

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)!

Если это Python 3, print теперь функция.Правильный синтаксис будет

print (recip)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top