Repository navigation
First Debug
Debugging means finding and fixing mistakes in your code. VS Code's debugger lets you pause your program, check variable values, and run one line at a time.
In this guide, you will find and fix one small mistake.
Use VS Code on your computer or in the course virtual desktop.
- Complete the course's Python and VS Code setup.
- Open your course folder using File > Open Folder.
- Make sure Microsoft's Python and Python Debugger extensions are installed and enabled.
Your course folder includes the debugger settings. You can use them without editing .vscode/launch.json.
Create a file named debug_practice.py in your course folder. Enter this code and save the file:
quantity = 3
price = 4
total = quantity + price
print(total)Three items cost $4 each. The expected total is 12. This program has a mistake that produces a different result.
Keep the debug_practice.py editor tab selected. The course debugger runs the Python file in the selected tab.
A breakpoint tells the debugger where to pause.
Click just to the left of the line number for:
total = quantity + priceA red dot appears. You can also place your cursor on that line and press F9.
Expected result: A breakpoint is set on line 3.
- Open Run and Debug from the left sidebar. Its icon shows a play button and a bug.
- In the dropdown at the top, select Debug Current Python File.
- Click back inside
debug_practice.py. - Choose Run > Start Debugging, or press F5.
Expected result: The program pauses at line 3. VS Code highlights that line and displays the debug toolbar.
The highlighted line has not run yet. It is the next line that will run.
In the VARIABLES section of Run and Debug, expand Locals if needed.
You should see:
-
quantityis3. -
priceis4. -
totaldoes not appear yet because its assignment has not run.
Before running line 3, predict its result: What does 3 + 4 produce?
Click Step Over on the debug toolbar, or press F10.
Expected result: The highlight moves to print(total), and total appears with the value 7.
That explains the problem: the code adds the quantity and price. To calculate the cost, it needs to multiply them.
Hover over a toolbar button to see its name. While paused, you can also hover over a variable in your code to check its value.
- Click Stop on the debug toolbar.
- Change
+to*in the calculation. - Save the file.
- Start debugging again.
- At the breakpoint, click Step Over.
Expected result: total is now 12.
Click Continue to finish the program. Open the Terminal tab at the bottom of VS Code.
Expected output:
12
Click the red breakpoint dot to remove it when you finish practicing.
| Control | What it does |
|---|---|
| Step Over | Runs the highlighted line, then pauses at the next line to execute. |
| Continue | Runs until another breakpoint, an error, or the end of the program. |
| Stop | Ends the debugging session. |
If a keyboard shortcut does not work, use the toolbar button.
If your code uses input(), select the Terminal tab, click beside the program's prompt, type your answer, and press Enter.
The program may appear to be waiting after you click Step Over on an input() line. Enter the requested input so it can continue.
| Problem | First thing to check |
|---|---|
| The debug configuration is missing. | Open the course folder that contains .vscode, rather than only an individual file or assignment subfolder. |
| VS Code runs the wrong file. | Stop debugging. Select the correct .py editor tab before starting again. |
| The program does not pause. | Check that the red breakpoint dot is on a line the program actually runs. |
| A variable is missing. | Its assignment may not have run yet. Step Over that assignment and check again. |
Python reports a SyntaxError. |
Read the error message and check the indicated line and the line before it. Fix and save the code before debugging again. |
If you are still stuck, send your instructor the relevant code, the complete error message, your expected result, and what actually happened.
- Identify the input and expected result.
- Set a breakpoint before the calculation or decision you want to check.
- Predict what the next line should do.
- Step Over and compare the actual values with your prediction.
- Stop, make one small change, save, and test again.
A program finishing without an error does not guarantee the answer is correct. Always compare its output with the expected result.
Wiki Home · Code Repository · Ask a Question · Report an Issue
Maintained by GC STEM.