Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to see error in simple python code [duplicate]

Tags:

python

I am trying the code in the tutorial given here - http://www.vogella.com/tutorials/Python/article.html

He uses python 2.6 and I use 3.3. I don't know if that causes my problem.

My code -

def add(a,b):
    return a+b 

def addFixedValue(a):
    y = 5
    return y+a

print add(1,2)
print addFixedValue(1)

Error is -

    print add(1,2)
            ^
SyntaxError: invalid syntax

How do I correct it ?

like image 962
InTheSkies Avatar asked Aug 17 '26 17:08

InTheSkies


1 Answers

In Python versions before 3, print was a statement, not a function, so the code as you have it here is correct.

Starting with Python 3, print is now a function, so you need to wrap arguments in parentheses.

Python 1.x - 2.x:

print "This is a string"

Python 3.x:

print("This is a string")

The rationale behind this change is explained at http://legacy.python.org/dev/peps/pep-3105/

like image 148
bgporter Avatar answered Aug 19 '26 07:08

bgporter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!