views:

693

answers:

2

I am trying to learn Python, however I tried to run a script that is LITERALLY just:

print "Hello, World!"

And I get this error:

  File "hello.py", line 1
    print "Hello, World!"
                        ^
SyntaxError: invalid syntax

What is going on!?

+29  A: 

try:

print("Hello, World!")

You are probably using Python 3.0, which now requires parentheses.

Unknown
Thank you, this worked. I don't know why this isn't more common knowledge, because I just copy-pasted from the first Google result for Python Hello World.
MiffTheFox
"requires parenthesis" is not really the adequate explanation as to the change from 2.x to 3 :)
Paolo Bergantino
@MiffTheFox: Python 2.x uses print as a statement. The relatively new Python 3 made print a function instead. The majority of Python programmers are still using 2.x because of its extensive library and framework support, so 3.0 isn't nearly as adopted as you'd expect for now.
Paolo Bergantino
@paulo, its the most succinct. If I had said, it is now a function, I would have to then explain what the difference between a statement and an expression is and how a function fits into the whole picture.
Unknown
operator vs function
CrazyJugglerDrummer
They should have a special error message for cases like this with a bit more explanation. With all the documentation out there for Python 2, this kind of incompatible syntax change is bound to frustrate the uninitiated a lot.
Thilo
Well, this certainly seems like a step in the wrong direction!
Andrew Johnson
@Andrew: in what sense is this a step in the wrong direction? Now that print is a function, it can be treated like all other functions (and can thus be passed around, etc.)
Stephan202
+14  A: 
Christian
Blixt