What To Do If Your Program Crashes
If your program crashes during runtime (when else :-) there are a few
ways to deal with this. Solution 1 is works for all programming
languages and has been used by programmers "forever".
Works for all programming languages:
Solution 0: Engage gray matter -- get a hardcopy of your code and
*think* hard.
Solution 1: "pepper" your code with print statements (in the correct
syntax for the language - I'm using Python here). Simple
things like:
... some code here ...
print 'I got here #1 @@@'
... some code here ...
print 'I got here #2 @@@'
... some code here ...
print 'I got here #3 @@@'
etc. will help. The numbering will give you a way to know
which of your trace print statements executed. If it gets
to #1 but not #2 you have some idea where it crashes. You
can then move some print statements in the region between
the first and second print statement.
By the way, the "@@@" will let you *easily* find all of
your trace statements later and delete them *before* you
turn in your code.
Solution 2: Print out the value of variables to confirm that they do
contain what you think they do. Echo back (i.e., print)
values of variables you assigned based on user input. So
if you have something like:
answer = input("Enter a value now")
print 'answer contains: ', answer # to verify the variable
# contains the value you expect
Solution 3: Use a debugger (we'll talk about these later)
Solution 4: Explain your code to someone else - often when you
verbalize what you are doing your problem/mistake becomes
obvious. It really works, even if you speak out loud to
yourself.
Python/IDLE specific:
Solution 5: When a Python program crashes it generates a 'traceback' -
it's worth reading. It will tell a lot, the line number
and name of the method where a problem occurred, and what
the problem was that caused the ultimate demise of your
program. See this link for how to interpret
a traceback.
Finally, if you right-click on the traceback, it will pop
up a small menu that will allow you to jump to the
corresponding line shown. Also, IDLE shows the current
line number in the lower right edge of the window.