0

So I wrote a very simple python test file called test testProg.py, and it looks like this:

import sys

def adder(a, b):
    sum = a+b
    print sum

if __name__ == "__main__":
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    adder(a, b)

From another question here, I did the command:

python testProg.py 3 4

However I get the following error message:

 File "testProg.py", line 5
    print sum
            ^
SyntaxError: invalid syntax

I am honestly not sure what the issue it... I can run python from the command prompt easily with no issue, but why cant I replicate that questions' solution?

Thanks.

Edit: Python 3.4 is used

Community
  • 1
  • 1
TheGrapeBeyond
  • 543
  • 2
  • 8
  • 19

1 Answers1

4

It looks like you have Python 3 installed. The code you are running was written for Python 2, which has slightly different syntax. For this example, you need to change that to print(sum). In general, you should search around for information on the difference between Python 2 and 3, and be careful to note what version is used in code you find on the internet. Code written for Python 2 will often not run as-is on Python 3.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • ...Python < 3 has syntax THAT different from python >= 3 !? Wow! – TheGrapeBeyond May 29 '14 at 20:02
  • 1
    @andi Im a noob to Python, but this incident leaves a very sour taste in my mouth... why would they pull syntax from under our feet like that? What the hell? > – TheGrapeBeyond May 29 '14 at 20:06
  • @TheGrapeBeyond: The syntax is not super-different -- in this example, all you have to do is add parentheses -- but yes, it can be annoying. The main problem, though, isn't the change in syntax itself -- they provided ways in Python 2 to get the Python 3 syntax to ease the transition. The problem is that, when you find code on the internet, it's not always obvious which version it was written for, unless it's explicitly stated on the page where you find it. – BrenBarn May 29 '14 at 20:10