[Tutor] (no subject)

Steven D'Aprano steve at pearwood.info
Sat Sep 7 01:44:29 CEST 2013


On Fri, Sep 06, 2013 at 02:52:51PM -0500, Sammy Cornet wrote:

> on my Interpreter windows, as my first attempt, I wrote "hello world" 
> but it keep telling me this: SyntaxError: invalid syntax
>  Can you help me please?

Yes. The first, and most important, tool in your toolbox as a programmer 
is to take careful note of the error messages you are given. The second 
most important tool is to ask good questions.

Asking good questions means showing exactly what you did, *precisely*, 
and not leaving it up to the reader to guess. The best way is to copy 
and paste the relevant lines from your interpreter window.

If you typed *literally* "hello world", complete with quotation marks, 
you shouldn't have got a SyntaxError, so I'm guessing you didn't do 
this:

py> "hello world"
'hello world'


But if you left out the quotation marks, you would have:

py> hello world
  File "<stdin>", line 1
    hello world
              ^
SyntaxError: invalid syntax


Notice the almost-blank line above the SyntaxError? See the ^ caret? 
That shows you where Python thinks the error is. Unfortunately the 
Python parser is a bit dumb, and often doesn't detect the error until 
the end of the offending text, rather than the beginning. But in this 
case, it doesn't matter: the offending term is "world" (no quotation 
marks) since you cannot separate what looks like two variables with just 
a space.

`"hello"` inside quotation marks is a string, `hello` on its own without 
quotation marks is a variable. If you leave the quotation marks out, 
then Python parses your text as:

(variable hello) (variable world)

which is illegal syntax.

Have I guessed you problem correctly? If not, you'll have to show us 
exactly what you did. In the meantime, here's my second guess: you tried 
to *print* "hello world", and you did this:

print "hello world"


In this case, are you using Python 3? In Python 2, print is a statement, 
and can be given as shown above, but that was deemed to be a mistake for 
various reasons, and in Python 3 it was changed to a regular function 
that requires round brackets (or parentheses for American readers):

print("hello world")


Does this help?


-- 
Steven


More information about the Tutor mailing list