Developers often have different ways they approach the troubleshooting process. Regardless of what process you are comfortable with, it helps to be consistent and follow the same set of steps.
- Analyze any Python error messages to try to identify the exact problem
- Check spelling, casing, variable naming and mis-naming
- Check missing characters such as colons, parenthesis, and quotes
- Review indentation and scope issues
- Comment code (to disable it) and use print() statements to track down issues on specific lines
Common Errors: Analyzing an Exception
Sometimes Python will try to tell you exactly what is wrong and where it is happening. Examine this image of a program exception:
Here's the relevant section of code (line 18):
The error message is actually not very clear. But it does give you an idea of where to look. In this case, the statement is missing an ending single quote character (and a right parenthesis).
Here is another common error (indentation):
Python gets the line correct, but isn't very helpful in terms of the problem. However, it does indicate that the problem is "Indentation". Remember that Python is very stringent about indentation. If necessary, review the rules about indentation.
Missing Colons: Python will always catch this error, but the error message is not helpful. Remember that functions and control statements (if, else, while) all require a colon at the end of the statement.
Unfortunately, Python cannot always determine what the problem is, or sometimes it gets it wrong. So, it is often necessary to conduct your own troubleshooting steps. Here are a suggested set of steps:
- Check spelling in recent changes (hopefully you have only edited 2-4 lines recently)
- Check casing in recent changes; Python is case-sensitive. "newAmount" and "newamount" are not the same thing. They would declare 2 different variables. Python may not flag this as an error.
- Look for missing quotes around strings
- Look for missing parenthesis, where used, such as in print statements
- Check control statements to make sure they end in a colon (this is easy to forget for functions, which are def statements)
- Check indentation under control statements and functions. Also, statements outside of control statement blocks should NOT be indented
- Display the value of variables using print() statements. In other words, make sure the data is what you think it is.
- Make sure you have declared variables before using them.
- If using a loop, use print() statements to make sure data from an array or file is what you think it is.
- Check to make sure variables are "within scope". More on this below.
- Remember that arrays are zero-based. Make sure you are attempting to access the correct array cell, and not a cell that is larger than the array itself.
If necessary, comment out parts of the program and re-test until you find the section causing the error. Comment out individual lines, if necessary.
For more advanced programs, it is sometimes helpful to place a print statement at the beginning of every function, and within control statements, so you know what parts of the program are executing when the error occurs.
Sample of Error Not Caught By Python
Python will catch syntax errors, and some spelling errors, but cannot catch many types of programmer errors. Review the case below.
Python Scope and Scope Errors
Scope errors often occur in programs using functions. These are not always easy to troubleshoot, because the cause is not obvious. Python will throw an error for an undeclared variable, but that only describes part of the problem. Check out the Handling Scope page to review scope issues.
Sometimes, it appears the variable is actually declared, but due to scope, there may be variables with the same name declared in different places - and that are actually different variables.
One way to troubleshoot possible scope issues is to display (print) the values submitted to functions as parameters, the value of any variable being returned from a function, and the value of any value after it is returned from a function. These print() statements can always be removed later, but while testing they help track what the values are for all variables (and parameters) to make sure the program has what you think it has during execution.