views:

192

answers:

4

My code:

print "Hello World!"

I even tried adding a semicolon behind, but everytime I save and run (as Python run) it says:

File "E:\Software\Eclipse\Workspace\Python1\src\main.py", line 1 print "Hello World!";

SyntaxError: invalid syntax

I have no idea why.

A: 

In Python, indentation is really important... Have you check your indentation? Also, lose the ; (don't need it).

correct:

print("hello") or print "hello" (for < 3.0)

not correct:

...print("hello") or print "hello" (for < 3.0)

where . denotes spaces.

jldupont
There are no spaces/tabs in front if that's what you mean.
Fabian
+1  A: 

This is kind of a longshot but - if you're running python 3.0 that is invalid syntax. Try

print("Hello World!")

to see if this is the case.

Mongoose
+6  A: 

What version of Python are you using? Python 2.X has print as a keyword, but Python 3.X only has print() as a function - you'd need to use print("Hello, World!") instead.

Amber
Ah, I see. Problem solved. Just curious, is it a must to have a semi-colon at the end of every line as in the case of PHP/Javascript? Hm, maybe I should have used the latest 2.X instead of 3.X, I thought 3.X would be better since it's newer.
Fabian
Python does not require semicolons (and in fact discourages them).
Amber
For a more comprehensive list of differences, take a look at http://docs.python.org/dev/3.0/whatsnew/3.0.html
Amber
A: 

Thanks for this thread -- I thought I was going crazy!

I think a compiler error like, "obsolete syntax, use print()" would have been helpful

abe