views:

334

answers:

3

Why is this complaining about an invalid syntax?

#! /usr/bin/python

recipients = []
recipients.append('[email protected]')

for recip in recipients:
    print recip

I keep getting:

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

If you are using Python 3 print is a function. Call it like this: print(recip).

Nikola Smiljanić
I'm guessing python 3.0 has a lot of breaking changes? Seems like raw_input no longer works in favor of input too. Hard for a beginner to follow examples when they're in incompatible versions. :(
Chris
All 3.x changes are listed in "What's new" sections of 3.0 and 3.1 docs.
PiotrLegnica
If you're learning from examples based on 2.6, you should be using 2.6. It's still available.
recursive
@Chris, the breaking changes are the whole reason why it's Python 3 instead of Python 2.8 or something. Overall the changes are very reasonable, and everything is well-documented as noted above. Python 3 cleans up some warts that have been in Python for years, and makes some performance improvements, in ways that *had* to break a few things that used to work. Overall it's worth it. And they have no plans to break things again anytime soon; in fact there is a temporary freeze on new features, in order to give people time to assimilate the new Python 3 stuff.
steveha
@steveha: I get the reasoning for it. I was just commenting on the status of the internet and the fact that most of the python examples out there use code written for the 2.x codebase. I'll just have to read up on the "What's New" info and go from there, no biggie.
Chris
*"status of the internet"*. Now That's amusing! not much we can do.. we should talk more about Py 3 on stackoverflow. Read the Official tutorial on Python, it's up to date: http://docs.python.org/3.1/tutorial/
kaizer.se
+3  A: 

If it's Python 3, print is now a function. The correct syntax would be

print (recip)
eduffy
+4  A: 

In python 3, print is no longer a statement, but a function.

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

More python 3 print functionality:

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