[Tutor] What's the invalid syntax? [What's the error mesaage?]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Jul 10 08:29:16 CEST 2005

On Sat, 9 Jul 2005, Nathan Pinno wrote:

> What's the invalid syntax?
> Here's the code (Part of my Guess the Numbers game):
>         if a0 == x0 and a1 == x1 and a2 == x2 and a3 == x3:
>             print "Congratulations! Way to go?"
>             answer = raw input("Play again: (Y)es or (N)o Type the letter of your choice. ")

Hi Nathan,

Next time you ask this kind of question, show us the error message.
Brian has asked you before on other questions in the past; his
recommendation is a good one in general.

Error message are not content-less, despite what you might think.  They
usually have some kind of useful information associated with them, and
they they really often provide key clues to what's broken.

Let's try an example to demonstrate this idea.  Let's say that we write a
program like this:

def test_syntax_error():
    print "hello world"
    goodbye world

When we run this, Python says that there's a problem here.  But it
actually says more than "it doesn't work"; it gets specific:

  File "<stdin>", line 3
    goodbye world
SyntaxError: invalid syntax

Python is saying: "Up to line 2 of the program, things look syntactically
ok.  I, the Python system, hit a problem on line three.  Here, I'll show
the line to you; maybe you'll see what's wrong immediately.  Look around
there for the syntax error.  If it helps here's more info: I got confused
as soon as I saw the word 'world'; I was not expecting that word there."

Of course, if Python really did say something like that in full English,
it would be too verbose.  So it does take some practice in reading
something terse like an error message, and actually understanding what it
means.  But many of us on the tutor list have that experience.

I hope this makes it clear: if you see an error message, include it!
It's actually really useful for us when we try to help you with problems.

More information about the Tutor mailing list