This page intentionally left blank Python for Software Design



tải về 1.38 Mb.
Chế độ xem pdf
trang79/83
Chuyển đổi dữ liệu13.08.2023
Kích1.38 Mb.
#55046
1   ...   75   76   77   78   79   80   81   82   83
- Python for Software Design How to Think Like a Computer Scientist-Cambridge University Press (2009)

Appendix
Debugging
Different kinds of errors can occur in a program, and it is useful to distinguish among
them in order to track them down more quickly:

Syntax errors are produced by Python when it is translating the source code into
byte code. They usually indicate that there is something wrong with the syntax of
the program. Example: Omitting the colon at the end of a def statement yields
the somewhat redundant message SyntaxError: invalid syntax.

Runtime errors are produced by the interpreter if something goes wrong while
the program is running. Most runtime error messages include information about
where the error occurred and what functions were executing. Example: An infi-
nite recursion eventually causes the runtime error “maximum recursion depth
exceeded.”

Semantic errors are problems with a program that runs without producing error
messages but doesn’t do the right thing. Example: An expression may not be
evaluated in the order you expect, yielding an incorrect result.
The first step in debugging is to figure out which kind of error you are dealing with.
Although the following sections are organized by error type, some techniques are
applicable in more than one situation.
A.1
SYNTAX ERRORS
Syntax errors are usually easy to fix once you figure out what they are. Unfor-
tunately, 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 nec-
essarily where the error is. Sometimes the error is prior to the location of the error
message, often on the preceding line.
231


232
Debugging
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:
(i) Make sure you are not using a Python keyword for a variable name.
(ii) Check that you have a colon at the end of the header of every compound
statement, including for, while, if, and def statements.
(iii) Make sure that any strings in the code have matching quotation marks.
(iv) 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
tải về 1.38 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   75   76   77   78   79   80   81   82   83




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương