Back to Resources

Debug Python with GitHub Copilot

Clone GitHub's debug-with-copilot repo, reproduce a TypeError and a bad factorial, ask Copilot Chat, apply the patch, and prove fail-then-pass in Python.

What are we building and why?

We are learning to debug with GitHub Copilot on two intentional Python bugs: one that crashes with a TypeError, and one that finishes but prints the wrong factorial. The outcome is a working habit: run the code, capture the failure, ask Copilot to explain and fix, then apply the change yourself.

GitHub's Learning to debug with GitHub Copilot page clones new2code/debug-with-copilot in VS Code, runs bugged_dice_battle.py and bugged_factorial_finder.py from the integrated terminal, and pastes fixed Chat prompts. There are two failure shapes: the process exits with an error, or it exits cleanly with output that is clearly wrong. Copilot helps in both cases.

When we ran these teaching files at ZeroShot Studio, the dice script failed immediately with TypeError: can only concatenate str (not "int") to str, and the factorial script printed a number far above 720 (6 factorial). After applying Copilot's str() conversion and correcting the *= loop, dice printed a Player line and factorial printed 720. We cut "stare at the traceback for 20 minutes" down to a few minutes of Chat plus a careful edit. The limitation: Copilot does not automatically write the file for you in this exercise. You still paste or accept the fix.

Related reading: How to Set Up Copilot for Learning to Code, How to Develop Your Project Locally, and How to Get Feedback on Your Code from GitHub Copilot. Authority: the GitHub learning page, Python downloads, and Python in Visual Studio Code.

"Finding and fixing bugs in code can be frustrating, especially when you're a new developer."

That is GitHub's opening. My rule of thumb, the one we use at ZeroLabs: reproduce first, paste the exact error or the expected-versus-actual output second, then change one thing at a time.

Flowchart
8 linescompact
flowchart TD
    Run[Run the Python file] --> Shape{Crash or wrong output?}
    Shape -->|Crash| Err[Copy the error message]
    Shape -->|Wrong output| Ctx[Describe expected vs actual]
    Err --> Chat[Paste Copilot Chat prompt]
    Ctx --> Chat
    Chat --> Fix[Apply the suggested code change]
    Fix --> Rerun[Run the file again]
Rendered from Mermaid source with the native ZeroLabs diagram container.

What are the required prerequisites?

GitHub lists three hard requirements for the examples in this article.

Prerequisite LayerMinimumProduction recommendationPurpose in stack
VS Code + CopilotCopilot Chat in VS CodeFollow Set up VS Code with CopilotClone dialog, terminal, Chat prompts
Python runtimeCurrent supported Python 3Installer from python.orgRun the two teaching scripts
Python extensionOfficial VS Code Python extensionLatest stable from the Extensions viewLanguage support while you edit
Teaching reponew2code/debug-with-copilotFresh clone opened in VS CodeShips bugged_dice_battle.py and bugged_factorial_finder.py
  • Complete Copilot setup in VS Code before you start.
  • Download Python and confirm py (Windows) or python / python3 (macOS/Linux) works in a terminal.
  • Install the Python extension for Visual Studio Code.
  • You do not need a separate virtualenv for this teaching exercise unless you already prefer one.

When we tested on macOS, python3 bugged_dice_battle.py was the working launcher. On Windows the guide uses py. Use the launcher GitHub documents for your OS so the traceback matches the exercise.

How do you implement the step-by-step recipe?

Work through both teaching files. Keep the same terminal open for both runs.

  1. Clone the example repository in VS Code. Start cloning new2code/debug-with-copilot. Choose a save location, click Select as Repository Destination, and open the repository when prompted.

  2. Run the crashing dice battle file. Open and review bugged_dice_battle.py. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P), run Terminal: Create New Terminal, then run the OS-specific command.

    Windows:

    Terminalbash
    py bugged_dice_battle.py

    macOS or Linux:

    Terminalbash
    python bugged_dice_battle.py

    (If python is missing on your Mac or Linux install, try python3 with the same filename.) Press Enter. The terminal should end with:

    text
    TypeError: can only concatenate str (not "int") to str
  3. Ask Copilot to explain and fix the TypeError. Open Copilot Chat and send exactly:

    text
    Explain in depth why my code produces the following error and how I can fix it:TypeError: can only concatenate str (not "int") to str

    Copilot should explain that die_1 and die_2 are integers being concatenated to strings, and that you can only concatenate strings to strings. It should suggest wrapping the integers with str() before concatenation. Apply that suggestion in the file and save.

  4. Run the factorial file that produces wrong output. Open and review bugged_factorial_finder.py. In the same terminal run:

    Windows:

    Terminalbash
    py bugged_factorial_finder.py

    macOS or Linux:

    Terminalbash
    python bugged_factorial_finder.py

    The program should print a value much higher than 720, which is the correct value of 6 factorial.

  5. Ask Copilot why the output is too high. Send:

    text
    Why is the output of this code so much higher than expected? Please explain in depth and suggest a solution.

    Copilot should point out that the *= operator multiplies factorial by both i and an extra factorial each loop iteration. Apply the suggested fix (remove the extra factorial from the equation, or change *= to = as Copilot describes) and save.

  6. Reuse the same prompts on your own project. For a crash:

    text
    Explain in depth why my code produces the following error and how I can fix it:YOUR-ERROR-MESSAGE

    For wrong output, ask why the output is incorrect and how to fix it. Give as much context as you can about how the output differs from what you expected. For harder scenario prompts later, see GitHub's Debug errors guidance.

How do you verify the deployment works?

CheckExpected signalIf it fails
CloneBoth bugged_*.py files visible in VS CodeRe-run the VS Code clone dialog
Dice before fixTerminal ends with the TypeError aboveConfirm you ran the bugged file, not a fixed copy
Dice after fixScript exits without TypeError and prints a Player result lineRe-read Copilot's str() suggestion and save the file
Factorial before fixPrinted value is far above 720Confirm you ran bugged_factorial_finder.py
Factorial after fixPrinted value is 720Re-apply the loop fix Copilot described

Verification here is re-running the same py / python commands after each fix. Expected after both fixes: dice completes cleanly, factorial prints 720.

What are the common production failure modes?

  • python: command not found on macOS/Linux: Cause: only python3 is installed. Fix: run python3 bugged_dice_battle.py (and the factorial file the same way), or install Python from python.org so python exists.
  • Copilot answer is vague: Cause: you omitted the error text or the expected value. Fix: paste the full TypeError line, or say you expected 720 and got the larger number.
  • You "fixed" dice but factorial still wrong: Cause: two separate bugs in two files. Fix: finish the dice Chat prompt, then run the factorial file and send the wrong-output prompt. Do not assume one Chat answer covers both files.
  • Silent wrong output with no traceback: Cause: the bug never raised. Fix: tell Copilot the expected value and the actual value. That context is what makes invisible bugs debuggable.
  • Applied suggestion without re-running: Cause: trusting Chat without a second run. Fix: run the file again and confirm the new output before moving on.

FAQ

What are the two debugging situations GitHub highlights? Your code exits early with an error message, or your code finishes but the output is not what you expected. Copilot can help with both.

Why does the dice example fail with a TypeError? Because integers are being concatenated to strings. Convert the integers with str() before concatenation, as Copilot suggests in the exercise.

Why is the factorial output so large? The buggy loop multiplies by an extra factorial each iteration when using *=. Copilot's fix removes that extra factor or adjusts the assignment.

Do I need Copilot Pro for this exercise? GitHub's page assumes Copilot Chat inside VS Code. Use the Copilot plan you already set up in the VS Code Copilot setup docs. The prompts themselves are plain Chat text.

Where do I go for harder error scenarios? GitHub points next to Debug errors for scenario-specific Chat prompts.

Share