This guide is intended to help you learn to fix your own programming problems. As you learn to program, you will
encounter various bugs or errors. Some of these are easy to identify and fix, while others will take some more time.
This document will be updated with further information and tips during the semester. |
Syntax errors can be detected before your program begins to run. These types of errors are usually typing mistakes, but can more
generally it means that there is some problem with the structure of your program.Syntax Error
![]() EOL stands for End Of Line. This error means that there was an open quote somewhere, but the line ended before a closing quote was found. Another type of syntax error will simply say invalid syntax. An invalid syntax error means that there is a line that python doesn't know what to do with. The last common type of syntax error you will likely encounter has to do with indention. You may see unindent does not match any outer indention level unexpected indent. Examples:
print "hello world
Solution: When you press OK on the dialog box. Python will attempt to highlight the offending line in your source code. You should use this location as a hint for where to start looking for your problem. First check the area highlighted. Then check the entire line. Lastly, check the line or lines before the line highlighted. The location marked is where Python noticed there was a problem, so the actual problem could come before! If you get an indention error, you should check that all of your lines of code are properly aligned in the correct columns. You can place you cursor at the start of each line and look at the col: indicator at the bottom right of IDLE to check this. Token Error (missing parenthesis
![]() EOF stands for End Of File. This error usually means that there was an open parenthesis somewhere on a line, but not a matching closing parenthesis. Python reached the end of the file while looking for the closing parenthesis. Example:
a = 3 + (4 + 5
Solution: When you press OK on the dialog box. Python will attempt to highlight the offending line in your source code. However, since it had reached the end of the file, it will highlight the last line in the file! You can type a right parenthesis at the end of the file, and IDLE will highlight the matching opening parenthesis. You should then be able to find out where your missing parenthesis should be. Remember to remove the extra closing parenthesis at the end of your file. If this doesn't work, you may have to scan your entire file to look for the problem. The best solution is to avoid this problem by running your program after you write every few lines of code. If you encounter this error, you can then check your most recent changes as a likely suspect.
|
Runtime errors occur as your program executes. Since Python is an interpreted language, these errors will not occur until
the flow of control in your program reaches the line with the problem.Name ErrorThis will be a common error you encounter. It will give you a Traceback message like this: Traceback (most recent call last): File "C:/Users/John/Documents/Teaching-BU/Python-debugging/test.py", line 7, in main() function. Similarly, the next two lines say that the error occurred on line 5, within main
, and that the line with the error is print hello . Lastly, the actual NameError says that global name 'hello'
is not defined.A NameError means that Python tried to use a variable or function name, such as hello based on a previous definition. If
it hasn't been defined at this point, you get the error.Usual Causes:
print hello
The following example leaves the s off dollars in the second line:
dollars = input("Enter dollars: ")
Solution: Usually, you can look at the last line mentioned in the TraceBack error. Place your cursor within idle and move it until you are on the correct line as indicated by the Ln: indicator in the bottom right of the editor. You should find the specific line quoted by the error message. In the case of a NameError, you can check if you typed the variable or function name correctly, if it should be in quotes, or if you should have defined it somewhere prior to the line. TypeErrorA TypeError you might encounter may look like this: File "C:/Users/John/Documents/Teaching-BU/Python-debugging/test.py", line 2, in or File "C:/Users/John/Documents/Teaching-BU/Python-debugging/test.py", line 2, in These errors are caused by not matching the correct number of substitutions into a string format. The string format in both cases requires 2 substitutions. The first error is caused by attempting to substitute 3 arguments, and the second error is caused by trying to substitute 1 argument. |
Semantic or logic errors are problems with the design of your program. These usually do not produce any error message, but
instead cause your program to behave incorrectly. These types of errors can be tricky to track down.
Logic errorsThese errors are often caused by accidentally using one variable in a place where a different variable is intended, or by simply doing some math incorrectly. Consider the following example:
apples = 0
The above code will execute, but it will not output the total number of apples picked. Instead, it will output the amount that was picked the last time! This simple example is easy to fix, but in a more complicated program it can be difficult to find such problems. Solution: Consider this hypothetical pseudocode for an input-process-output design pattern program:
|
print
to debug your code
Using extra print statements to display the value of your program's variables is a useful way to figure out
what's going on with your program. The following example only has one intermediate calculation, but the same concept applies
to more complicated programs.
pv exampleConsider this example from class: pv.py Here are the important parts of the code with an error introduced: fv = input("Enter the amount to be received in the future: ") r = input("Enter the rate of return (e.g. 0.05 for 5 percent): ") n = input("Enter the number of years: ") # calculate the pvfactor = 1 / (1+r)^n pvfactor = 1 / (1+r) * n # calcualte the pv = fv * pvfactor pv = fv * pvfactor # output the present value print "That's worth $", pv, "today." When we run this code, all we see is the resulting output: Enter the amount to be received in the future: 100 Enter the rate of return (e.g. 0.05 for 5 percent): 0.05 Enter the number of years: 10 That's worth $ 952.380952381 today.The final result is obviously completely wrong because the value today should be less than the future value! To try to find the problem, we can add a number of extra print statements to try to see what's going on within our program: fv = input("Enter the amount to be received in the future: ") r = input("Enter the rate of return (e.g. 0.05 for 5 percent): ") n = input("Enter the number of years: ") print "fv =", fv, "r = ", r, "n=", n # calculate the pvfactor = 1 / (1+r)^n pvfactor = 1 / (1+r) * n print "pvfactor = ", pvfactor # calcualte the pv = fv * pvfactor pv = fv * pvfactor # output the present value print "That's worth $", pv, "today." Now we can see the values inside our variables at each step: Enter the amount to be received in the future: 100 Enter the rate of return (e.g. 0.05 for 5 percent): 0.05 Enter the number of years: 10 fv = 100 r = 0.05 n= 10 pvfactor = 9.52380952381 That's worth $ 952.380952381 today. The first thing to check is that your inputs are properly stored in their correct variables. We see that the values for fv , r , and n are what we entered.Next, we look at the intermediate calculation of pvfactor. We know this should be a number smaller than 1, but for some reason, it is 9.5238. We now know to focus on this line of code to look for the problem. Now you see that the exponent operator was not typed correctly, resulting in an incorrect calculation. The line should be: pvfactor = 1 / (1+r) ** n
Now, with the change, we can see: Enter the amount to be received in the future: 100 Enter the rate of return (e.g. 0.05 for 5 percent): 0.05 Enter the number of years: 10 fv = 100 r = 0.05 n= 10 pvfactor = 0.613913253541 That's worth $ 61.3913253541 today. One more thing you can notice with this program if you experiment a bit more with other inputs. The pvfactor calculation
assumes that the user enters a floating point number. What happens if the user enters an integer such as 1, or 2? This line of code then
calculates using integer division and the result is zero! You never want to leave such decisions up to the user. Let's make that change and then comment out the extra print statements to get our final solution.
fv = input("Enter the amount to be received in the future: ") r = input("Enter the rate of return (e.g. 0.05 for 5 percent): ") n = input("Enter the number of years: ") # print "fv =", fv, "r = ", r, "n=", n # calculate the pvfactor = 1 / (1+r)^n pvfactor = 1.0 / (1+r) ** n # print "pvfactor = ", pvfactor # calcualte the pv = fv * pvfactor pv = fv * pvfactor # output the present value print "That's worth $", pv, "today." |
You may encounter times when you cannot track down an error that is causing your program to fail dramatically. There are a
few steps you can take to recover:
|