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

9
  • What version of python are you using? Commented May 29, 2014 at 20:00
  • @wnnmaw 3.4.0 I believe Commented May 29, 2014 at 20:01
  • you should use something like: return and not print Commented May 29, 2014 at 20:02
  • Well there's your problem, as BrenBarn says, print is a function in Python 3 Commented May 29, 2014 at 20:02
  • @wnnmaw Being a noob to python... wow - is 3.4.0 syntax is that different? Wth? Commented May 29, 2014 at 20:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

...Python < 3 has syntax THAT different from python >= 3 !? Wow!
@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: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.