Skip to content

Here’s something you might not know about computer programming: programmers spend most of their time debugging code, not writing it. As you grow as a programmer, you’ll learn some new functions and methods and data structures, but more importantly, you’ll improve your critical thinking skills and get better at debugging your own (and other people’s!) programs.

Here are some basic tools and tips for writing and debugging your own code.

Tool #1: 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 #2: 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 (type(variable)), or their lengths (len(variable)). This can help you figure out which variable is misbehaving, and where the error originates.

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. You can also use comments like bookmarks to mark places you need to edit.

Tool #4: Try to break it.

When you’re testing your program, don’t use the “correct” inputs and data. Use a wide variety of “wrong” inputs to make sure your program can handle exceptions and user error. For example, if your program is supposed to take text input, see how it handles numbers or symbols. Try to break your program during testing. That way, you’ll know where issues crop up so that you can find a way to handle them.

Tool #5: Read the error messages.

Python gives you nice, helpful messages telling you what kind of errors it finds and where in your program the errors are. Error messages can be kind of confusing, until you know to read them from the bottom up! The last line will tell you the type of error; the line before that will tell you where in your code the error happened; and the rest of the lines will trace back through your program, to which part called the function that had the error, and so on.

Some common types of errors include:

  • SyntaxError: This usually means you forgot to close parentheses or a set of quotes, you forgot a colon :, or you put a symbol somewhere Python wasn’t expecting it.
  • IndentationError: Either you indented something that shouldn’t have been indented, or you didn’t indent something that needed to be.
  • IndexError: This usually means you used an index value that doesn’t exist. (For example, your list has four items in it and you called on index 4…remember Python starts at 0.)
  • NameError: Either you tried to use a variable before you defined it (or didn’t define it at all), or you misspelled the name of a function or variable.
  • TypeError: This usually happens with numbers, when a number needs to be converted from an int to a str (or vice versa) for what you’re trying to do.

Once you know what kind of error you have and where it might be, go look at your program to try to figure out what’s gone wrong.

Pro tip: if you can’t find the error on the line Python pointed to, try looking at the previous line.

Try to write code below that generates each type of common error message, and use comments to tell yourself why it causes an error. It may seem counterintuitive, but knowing how to create an error also helps you understand how to avoid it!

Tool #6: Use try/except statements.

These are similar to if/else, 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: have the program try to run with the correct input type/format/value, and if that doesn’t work, you can change a variable type, reset it, or give the user an error message and ask for input again.

try:
    [default code]
except:
    [alternate code]

As your skills improve, you can make your except statements more complex—for example, responding differently to different types of errors.

Tool #7: Take a break.

Sometimes, you just can’t figure out why your program isn’t doing what you expected. Or you’re getting an error message that you can’t make sense of. Maybe you’re frustrated beyond belief! Take a deep breath, and step away from the computer. Go do something else for a little while. If you’re too stressed out, your brain has a hard time focusing on problem-solving. And, fun fact: your brain will keep working on the problem, even when you’re not actively thinking about it. You might find, when you come back to your computer later, that it’s easier to solve the problem this time.

 

These are only some of the tools you should always keep in your debugging toolbox. The most important tools, though, aren’t specific techniques. Patience, persistence, and creativity are vital for successful debugging. Don’t worry if you don’t think you’re a very patient, persistent, or creative person—programming will help you improve all of these skills!

PS – I recommend you bookmark this page or keep it open in an extra tab, because it will come in handy during future lessons.

Skip to toolbar