TheGrapeBeyond
TheGrapeBeyond

Reputation: 563

Cant make simple Python program run through Windows command prompt

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

Upvotes: 0

Views: 146

Answers (1)

BrenBarn
BrenBarn

Reputation: 251365

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.

Upvotes: 4

Related Questions