"print" Throws An Invalid Syntax Error In Python 3
Solution 1:
It sounds like Pydev/LiClipse is using Python 3 while Codeacademy is using python 2.x or some other older version. One of the changes made when python updated from 2.x to 3 was print is now a function.
Python 2:
print"stuff to be printed"
Python 3:
print("stuff to be printed")
Solution 2:
You must take into account the version in which you are working.
In Python 2 your code would look like this:
parrot = "Norwegian Blue"printlen(parrot)
In Python 3 your code would look like this:
parrot = "Norwegian Blue"print ( len(parrot) )
Solution 3:
It worked in CodeAcademy because their interpreter is a Python 2.7, where you didn't need the parenthesis because print
was a statement. In Python 3.0+, print
requires the parentheses because it's a function.
More information on what's different between Python 2.7 and 3.0+ can be found here:
Some of the sample differences with print on the above page:
Old: print"The answer is", 2*2New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newlineNew: print(x, end=" ") # Appends a space instead of a newline
Old: print# Prints a newlineNew: print() # You must call the function!
It's good to know the differences between both, in case you're working with legacy systems and the lot vs. in your private environment. In Python 2.7 and below, print()
works; however, omitting the ()
s does not work in Python 3.0+, so it's better to get into the habit of using them for print.
End of life for Python 2.7 is expected to be in 2020, so you have plenty of time anyway.
Solution 4:
In Python 3 print was changed to require parenthesis. CodeAcademy is probably using Python 2 and it looks like you're using Python 3.
https://docs.python.org/3/whatsnew/3.0.html#print-is-a-function
From the docs
Print Is A Function The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105). Examples:
Post a Comment for ""print" Throws An Invalid Syntax Error In Python 3"