Demo: Debugging Python with VS Code 🖥️🐞¶
Goal¶
Learn to use VS Code's graphical debugger to find and fix bugs efficiently.
Script Overview¶
demo3-bmi-calculator.py calculates a patient's BMI, categorizes it, and prints recommendations. It also contains intentional bugs for debugging practice.
Known Bugs in demo3-bmi-calculator.py¶
- Incorrect BMI formula: uses
weight / heightinstead ofweight / (height ** 2) - Typo: uses
catgoryinstead ofcategory, causing aNameError - Off-by-one error: causes an
IndexErrorwhen printing list elements
Setup¶
- Open
demo3-bmi-calculator.pyin VS Code. - Ensure the Python extension is installed.
- Use the Run and Debug panel or the Run > Start Debugging menu.
Key Debugging Features in VS Code¶
Note: You can customize keyboard shortcuts in VS Code: 1. Open Command Palette (⌘+Shift+P on Mac, Ctrl+Shift+P on Windows/Linux) 2. Type "Preferences: Open Keyboard Shortcuts" 3. Search for "debug" to find and customize debugging shortcuts
1. Setting Breakpoints¶
- Click to the left of a line number to add a breakpoint.
- The program will pause when it reaches this line.
2. Running in Debug Mode¶
- Click the Run and Debug button or use keyboard shortcuts:
- Windows/Linux:
F5 - Mac:
fn+F5or use the Run and Debug sidebar (⌘+Shift+D) and click the green play button
3. Stepping Through Code¶
- Continue:
- Windows/Linux:
F5 - Mac:
fn+F5or click the green play button - Step Over:
- Windows/Linux:
F10 - Mac:
fn+F10or use the Step Over button in the debug toolbar - Step Into:
- Windows/Linux:
F11 - Mac:
fn+F11or use the Step Into button in the debug toolbar - Step Out:
- Windows/Linux:
Shift+F11 - Mac:
fn+Shift+F11or use the Step Out button in the debug toolbar
4. Inspecting Variables¶
- Hover over variables to see their values.
- Use the Variables panel to watch all current variables.
- Add Watch expressions to track specific values.
5. Viewing the Call Stack¶
- See the sequence of function calls that led to the current line.
- Helps understand program flow and context.
6. Using Conditional Breakpoints¶
- Right-click a breakpoint > Edit Breakpoint.
- Add a condition, e.g.,
age < 0. - The program will pause only when the condition is true.
7. Debug Console¶
- Execute Python commands live while paused.
- Inspect or modify variables interactively.
Tasks¶
- Set a breakpoint inside the
main()function (e.g., on the linebmi = calculate_bmi(...)). - Run in debug mode and pause execution.
- Step into the
calculate_bmifunction. - Observe: The BMI value is suspiciously large or small.
- Fix: Change the formula to
weight / (height ** 2). - Continue to the BMI categorization and print statements.
- When the program crashes with a
NameError, - Inspect: The typo
catgory. - Fix: Rename it to
category. - Run again. When the program crashes with an
IndexError, - Inspect: The loop
for i in range(1, len(numbers))and the linenumbers[i+1]. - NOTE: This is a compound bug: it causes an
IndexErroron the last iteration and skips printing the first element of the list. - Fix: Change the loop to
range(len(numbers) - 1)or adjust indexing accordingly. - Use variable inspection, call stack, and stepping to understand program flow and confirm each fix.
- Rerun to ensure all bugs are fixed.
Expected Outcomes¶
- Confidently use VS Code's debugger to find and fix bugs.
- Understand how to pause, inspect, and control program execution.
- Use conditional breakpoints to focus on tricky cases.