Skip to main content

Foundations of Python Programming: Functions First

Section 3.7 Tips for Debugging

Debugging a program is a different way of thinking than writing a program. The process of debugging is much more like being a detective. Here are a few useful debugging strategies:
  1. Pay close attention to error messages. What clues do you get from the error message? Where does it say the problem occurred? Ask yourself how the problem indicated could have occurred given your program’s inputs.
  2. Comment out some lines of code to simplify the program. Commenting out some of the operations around where the error occurs can help you zero-in on the problem.
  3. Use print statements to print out variable values, user inputs, and intermediate calcaultion results so you can compare them to what you expected. Note this requires you to have an expectation in the first place! Do the work to figure out how your program should be working, then pinpoint where it starts to deviate from your expectation.
  4. Run your program with test inputs to check its functionality.
  5. Use your IDE! Most Integrated Development Environments (IDEs - programs that help you write code) include useful debugging tools that allow you to step through your code (like the Codelens feature of this book) and see how variable values are changing along the way. Most IDEs will automatically highlight syntax errors too.
You have attempted of activities on this page.