Learn how to use the pause-step Python Debugger on CodeHS
Debuggers are very useful tools for analyzing the execution of your program. Debuggers allow you to pause your program while it is running, and slowly step through the code line by line to see the flow of execution. You can also print out the values of each variable at each step of your program.
To use the CodeHS Debugger, you'll want to first go to your Account Settings and switch your preferred IDE from Original to Abacus.
The CodeHS IDE allows you to pause and step through your Python programs by entering "Debug Mode" in your editor settings:
Let's try out the debugger on a real program! Press "Run" to start the debugger, and "Step" to start stepping through the code
Debugger Commands
The debugger provides a few options for stepping through the code:
Note
When debugging Python programs, we generally want to use Next anytime we are on an import statement, or about to call a Python library function that we didn't write ourselves.
In general, you'll only want to use Step to step into a function call for a function you personally wrote.
Your Turn
Try using Step and Next in this program!
Printing Variables
While debugging your program, you can use the print function to print out the values stored inside variables in your code:
Your Turn
Step to line 10 of this program, then:
Now step to line 14 of this program, then:
Breakpoints
What if you want to pause your program on a specific line of code? This is where breakpoints come in handy.
While using the debugger, you can click on any line number to set a breakpoint at that line. When you press Run or Continue, the debugger will pause once it hits that line of code.
Your Turn
Try setting some breakpoints in the following program!
Click on a line number to set a breakpoint at that line.
Press Run to start the program and pause at the first breakpoint you set.
Press Continue to continue to the next breakpoint.
Note
Breakpoints must be put in place before you press Run.
If you modify your breakpoints while your program is running, you'll need to re-run your program in order to pause at your new breakpoints.
Curious to Learn More?
CodeHS is using the industry standard built-in Python debugger, PDB, to pause and step through your Python programs.
In this tutorial, you learned about using the following PDB commands:
These are the main commands you'll be using to debug Python programs, but you can check out all the PDB commands at your disposal by reading the PDB Documentation