Debugging Aids |
While a program is running, each statement is highlighted in the edit pane as it gets executed. Watching will allow you to trace the actual flow of control through your program.
Let's show this by using an example:
#this is a comment in Python
print "Hello World" #This is also a comment in Python
""" This is an example of a multiline
comment that spans multiple lines
...
"""
To illustrate using comments to find where a program defect resides
in a program, consider the following program fragment that, when
executed, is known to cause an execution problem:
jump_up() move_across() jump_down()To find out why Reeborg is going on some crazy path, we can turn the last two function calls into comments, like this:
jump_up() """ move_across() jump_down() """Because the interpreter ignores comments (that is, does not try to execute a comment), the move_across and jump_down functions will not be invoked. Admittedly, we could have simply deleted the calls but then we would have had to remember what we had written when it comes time to reinsert the code. If Reeborg "jumps up" as intended, we can move the """ down, like this:
jump_up() move_across() """ jump_down() """and see what happens. If we now discover that Reeborg does not behave as intended, we have narrowed down the bug to inside the move_across function. Once we have repaired the move_across function, we can remove all of the comment delimiters.
(NOTE: Once finished debugging, be sure to check that no code remains commented out. It is considered a bad practice to submit any program that still contains commented out code; see the article Code in Comments for a discussion on why this is a bad practice.)
Leave the problem alone and get some rest. Often even a short break will allow your mental processes to get back on track to finding the source of your problem! Some final advice: Often programming errors are hard to find but, once discovered, painfully obvious. Do not be too upset with yourself when you find these errors. If you find a "stupid" error, do not think "I'm really dumb"; instead think "I must be a genius to locate such an obscure and hard-to-spot error!" Granted, both are overstatements, but the second is closer to the truth than the first. |