Think Python: How to Think Like a Computer Scientist

(singke) #1

Syntax Errors


Syntax errors are usually easy to fix once you figure out what they are. Unfortunately, the
error messages are often not helpful. The most common messages are SyntaxError:
invalid syntax and SyntaxError: invalid token, neither of which is very informative.


On the other hand, the message does tell you where in the program the problem occurred.
Actually, it tells you where Python noticed a problem, which is not necessarily where the
error is. Sometimes the error is prior to the location of the error message, often on the
preceding line.


If you are building the program incrementally, you should have a good idea about where
the error is. It will be in the last line you added.


If you are copying code from a book, start by comparing your code to the book’s code
very carefully. Check every character. At the same time, remember that the book might be
wrong, so if you see something that looks like a syntax error, it might be.


Here are some ways to avoid the most common syntax errors:


1 . Make    sure    you are not using   a   Python  keyword for a   variable    name.

2 . Check   that    you have    a   colon   at  the end of  the header  of  every   compound    statement,
including for, while, if, and def statements.

3 . Make    sure    that    any strings in  the code    have    matching    quotation   marks.  Make    sure
that all quotation marks are straight quotes, not curly quotes.

4 . If  you have    multiline   strings with    triple  quotes  (single or  double),    make    sure    you
have terminated the string properly. An unterminated string may cause an invalid
token error at the end of your program, or it may treat the following part of the
program as a string until it comes to the next string. In the second case, it might not
produce an error message at all!

5 . An  unclosed    opening operator    —   (,  {,  or  [   —   makes   Python  continue    with    the next
line as part of the current statement. Generally, an error occurs almost immediately
in the next line.

6 . Check   for the classic =   instead of  ==  inside  a   conditional.

7 . Check   the indentation to  make    sure    it  lines   up  the way it  is  supposed    to. Python  can
handle space and tabs, but if you mix them it can cause problems. The best way to
avoid this problem is to use a text editor that knows about Python and generates
consistent indentation.

8 . If  you have    non-ASCII   characters  in  the code    (including  strings and comments),  that
might cause a problem, although Python 3 usually handles non-ASCII characters. Be
careful if you paste in text from a web page or other source.
Free download pdf