Skip to content

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 / height instead of weight / (height ** 2)
  • Typo: uses catgory instead of category, causing a NameError
  • Off-by-one error: causes an IndexError when printing list elements

Setup

  • Open demo3-bmi-calculator.py in 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+F5 or use the Run and Debug sidebar (⌘+Shift+D) and click the green play button

3. Stepping Through Code

  • Continue:
  • Windows/Linux: F5
  • Mac: fn+F5 or click the green play button
  • Step Over:
  • Windows/Linux: F10
  • Mac: fn+F10 or use the Step Over button in the debug toolbar
  • Step Into:
  • Windows/Linux: F11
  • Mac: fn+F11 or use the Step Into button in the debug toolbar
  • Step Out:
  • Windows/Linux: Shift+F11
  • Mac: fn+Shift+F11 or 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

  1. Set a breakpoint inside the main() function (e.g., on the line bmi = calculate_bmi(...)).
  2. Run in debug mode and pause execution.
  3. Step into the calculate_bmi function.
  4. Observe: The BMI value is suspiciously large or small.
  5. Fix: Change the formula to weight / (height ** 2).
  6. Continue to the BMI categorization and print statements.
  7. When the program crashes with a NameError,
  8. Inspect: The typo catgory.
  9. Fix: Rename it to category.
  10. Run again. When the program crashes with an IndexError,
  11. Inspect: The loop for i in range(1, len(numbers)) and the line numbers[i+1].
  12. NOTE: This is a compound bug: it causes an IndexError on the last iteration and skips printing the first element of the list.
  13. Fix: Change the loop to range(len(numbers) - 1) or adjust indexing accordingly.
  14. Use variable inspection, call stack, and stepping to understand program flow and confirm each fix.
  15. 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.

Notes