Skip to content

Here are some tools for your debugging toolkit. These tools are like a hammer, screwdriver, and measuring tape for a carpenter—you’re going to need them for almost every project.

Get a PDF version of this webpage!

Tool #1: Use the levels of problem solving.

By the time you’re writing your program (level 2) and running it (level 1), you should already have spent some time on level 4 making sense of the problem and level 3 outlining the solution. If you run into issues on the lower levels, try “zooming out” to a higher level of problem solving to help you make sense of what’s going on in the program and execution.

Tool #2: Take baby steps.

As you write your program, run it frequently to test it. If something’s not working right, try to fix it right away. This will save you having to search through your entire program later to try to find what’s behaving oddly. As you’re debugging, make only one change at a time, so that you know what works.

Tool #3: Use comments!

As you’re writing your code, use comments to organize your program and make note of what different bits are supposed to do. This will help you locate the source of errors more quickly.

Tool #4: Try to break it.

When you’re testing your program, don’t use the “right” inputs and data. Use a variety of “wrong” inputs to make sure your program can handle them. For example, if it’s supposed to take text input, see how it reacts to numbers or symbols. Friends are very helpful for this, because they don’t have all the expectations you do about your program, so they tend to be more creative in breaking it.

Tool #5: Read the error messages.

If Python finds an error while running your code, it will give you a nice, helpful message telling you what kind of error it is and where in your program it is. Learn more about interpreting error messages.

Tool #6: Add print() statements.

If you’re not exactly sure where your program is going wrong, add some print statements around where you think the issue might be happening. Print the current values of variables, their types, or their lengths (len(variable)). This can help you figure out which variable is misbehaving, and where the error originates.

Tool #7: Use try/except statements.

These are similar to conditionals, but the try section holds your default code, and except holds alternate code to use if the try section produces an error. This is a great way to catch errors based on user input.

try:
  <default code>
except:
  <alternative code>

Purple Circle Question Mark Icon - Transparent PNG &amp; SVG Vector FileTool #8: Ask yourself these questions:

What am I trying to do? What have I tried already? What else could I try? What would happen if…?

Skip to toolbar