0

can anybody please tell me why this is giving me syntax error in idle?

def printTwice(bruce):
    print bruce, bruce

SyntaxError: invalid syntax

Spencer Hire
  • 735
  • 3
  • 14
  • 32
  • Ah, just add a return there after the colon, don't know why it did that, the syntax error is the first bruce in the statements section – Spencer Hire Sep 26 '12 at 01:54
  • 4
    Check your version of Python. That is invalid in Python 3.x, because `print` is just a function. –  Sep 26 '12 at 01:54
  • http://stackoverflow.com/questions/7901436/python-print-syntaxerror –  Sep 26 '12 at 18:09
  • Since the other question has been closed as a duplicate of this, the reference to [PEP 3105](http://www.python.org/dev/peps/pep-3105/) which explains what happened had better be brought forward. This johnny-come-lately question should have been closed as a duplicate of [Python `print` SyntaxError](http://stackoverflow.com/questions/7901436/python-print-syntaxerror) rather than that question being closed as a duplicate of this. – Jonathan Leffler Sep 28 '12 at 06:45
  • possible duplicate of [Python Interactive Shell - SyntaxError with print](http://stackoverflow.com/questions/4531083/python-interactive-shell-syntaxerror-with-print) – Martijn Pieters Sep 28 '12 at 07:33

2 Answers2

5

Check the version of Python being used; the variable sys.version contains useful information.

That is invalid in Python 3.x, because print is just a normal function and thus requires parenthesis:

# valid Python 3.x syntax ..
def x(bruce): print(bruce, bruce)
x("chin")

# .. but perhaps "cleaner"
def x(bruce):
    print(bruce, bruce)

(The behavior in Python 2.x is different, where print was a special statement.)

Community
  • 1
  • 1
3

You seem to be trying to print incorrectly.

You can use a Tuple:

def p(bruce):
    print (bruce, bruce) # print((bruce, bruce)) should give a tuple in python 3.x

Or you can use formatting in a string in Python ~2.7:

def p(bruce):
    print "{0}{1}".format(bruce, bruce)

Or use a function in Python 3:

def p(bruce):
    print("{0}{1}".format(bruce, bruce))
Amelia
  • 2,967
  • 2
  • 24
  • 39