This page intentionally left blank Python for Software Design


part of the program as a string until it comes to the next string. In the second



tải về 1.38 Mb.
Chế độ xem pdf
trang80/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)


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!
(v) 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.
(vi) Check for the classic = instead of == inside a conditional.
(vii) 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.
If nothing works, move on to the next section
. . .
A.1.1
I Keep Making Changes and It Makes No Difference
If the interpreter says there is an error and you don’t see it, that might be because
you and the interpreter are not looking at the same code. Check your programming
environment to make sure that the program you are editing is the one Python is
trying to run.
If you are not sure, try putting an obvious and deliberate syntax error at the beginning
of the program. Now run it again. If the interpreter doesn’t find the new error, you
are not running the new code.
There are a few likely culprits:

You edited the file and forgot to save the changes before running it again. Some
programming environments do this for you, but some don’t.

You changed the name of the file, but you are still running the old name.

Something in your development environment is configured incorrectly.

If you are writing a module and using import, make sure you don’t give your
module the same name as one of the standard Python modules.


A.2 Runtime Errors
233

If you are using import to read a module, remember that you have to restart the
interpreter or use reload to read a modified file. If you import the module again,
it doesn’t do anything.
If you get stuck and you can’t figure out what is going on, one approach is to start
again with a new program like “Hello, World!,” and make sure you can get a known
program to run. Then gradually add the pieces of the original program to the new one.
A.2
RUNTIME ERRORS
Once your program is syntactically correct, Python can compile it and at least start
running it. What could possibly go wrong?
A.2.1
My Program Does Absolutely Nothing
This problem is most common when your file consists of functions and classes but
does not actually invoke anything to start execution. This may be intentional if you
only plan to import this module to supply classes and functions.
If it is not intentional, make sure that you are invoking a function to start execu-
tion, or execute one from the interactive prompt. Also see the “Flow of Execution”
section below.
A.2.2
My Program Hangs
If a program stops and seems to be doing nothing, it is “hanging.” Often that means
that it is caught in an infinite loop or infinite recursion.

If there is a particular loop that you suspect is the problem, add a print state-
ment immediately before the loop that says “entering the loop” and another
immediately after that says “exiting the loop.”
Run the program. If you get the first message and not the second, you have got
an infinite loop. Go to the “Infinite Loop” section below.

Most of the time, an infinite recursion will cause the program to run for a while
and then produce a “RuntimeError: Maximum recursion depth exceeded” error.
If that happens, go to the “Infinite Recursion” section below.
If you are not getting this error but you suspect there is a problem with a recursive
method or function, you can still use the techniques in the “Infinite Recursion”
section.

If neither of those steps works, start testing other loops and other recursive
functions and methods.

If that doesn’t work, then it is possible that you don’t understand the flow of
execution in your program. Go to the “Flow of Execution” section below.
A.2.2.1
Infinite Loop
If you think you have an infinite loop and you think you know what loop is causing
the problem, add a print statement at the end of the loop that prints the values of
the variables in the condition and the value of the condition.


234
Debugging
For example:
while x > 0 and y < 0 :
# do something to x
# do something to y
print
"x: ", x
print
"y: ", y
print
"condition: ", (x > 0 and y < 0)
Now when you run the program, you will see three lines of output for each time
through the loop. The last time through the loop, the condition should be false. If
the loop keeps going, you will be able to see the values of x and y, and you might
figure out why they are not being updated correctly.
A.2.2.2
Infinite Recursion
Most of the time, an infinite recursion will cause the program to run for a while and
then produce a Maximum recursion depth exceeded error.
If you suspect that a function or method is causing an infinite recursion, start by
checking to make sure that there is a base case. In other words, there should be some
condition that will cause the function or method to return without making a recursive
invocation. If not, then you need to rethink the algorithm and identify a base case.
If there is a base case but the program doesn’t seem to be reaching it, add a print
statement at the beginning of the function or method that prints the parameters.
Now when you run the program, you will see a few lines of output every time the
function or method is invoked, and you will see the parameters. If the parameters
are not moving toward the base case, you will get some ideas about why not.
A.2.2.3
Flow of Execution
If you are not sure how the flow of execution is moving through your program, add
print
statements to the beginning of each function with a message like “entering
function foo,” where foo is the name of the function.
Now when you run the program, it will print a trace of each function as it is invoked.
A.2.3
When I Run the Program I Get an Exception
If something goes wrong during runtime, Python prints a message that includes the
name of the exception, the line of the program where the problem occurred, and a
traceback.
The traceback identifies the function that is currently running, and then the function
that invoked it, and then the function that invoked that, and so on. In other words,
it traces the sequence of function invocations that got you to where you are. It also
includes the line number in your file where each of these calls occurs.


A.2 Runtime Errors
235
The first step is to examine the place in the program where the error occurred and see
if you can figure out what happened. These are some of the most common runtime
errors:

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