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 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]What are the required prerequisites?
GitHub lists three hard requirements for the examples in this article.
| Prerequisite Layer | Minimum | Production recommendation | Purpose in stack |
|---|---|---|---|
| VS Code + Copilot | Copilot Chat in VS Code | Follow Set up VS Code with Copilot | Clone dialog, terminal, Chat prompts |
| Python runtime | Current supported Python 3 | Installer from python.org | Run the two teaching scripts |
| Python extension | Official VS Code Python extension | Latest stable from the Extensions view | Language support while you edit |
| Teaching repo | new2code/debug-with-copilot | Fresh clone opened in VS Code | Ships bugged_dice_battle.py and bugged_factorial_finder.py |
- Complete Copilot setup in VS Code before you start.
- Download Python and confirm
py(Windows) orpython/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.
-
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. -
Run the crashing dice battle file. Open and review
bugged_dice_battle.py. Open the Command Palette (Ctrl+Shift+PorCmd+Shift+P), run Terminal: Create New Terminal, then run the OS-specific command.Windows:
py bugged_dice_battle.pymacOS or Linux:
python bugged_dice_battle.py(If
pythonis missing on your Mac or Linux install, trypython3with the same filename.) Press Enter. The terminal should end with:TypeError: can only concatenate str (not "int") to str -
Ask Copilot to explain and fix the TypeError. Open Copilot Chat and send exactly:
Explain in depth why my code produces the following error and how I can fix it:TypeError: can only concatenate str (not "int") to strCopilot should explain that
die_1anddie_2are integers being concatenated to strings, and that you can only concatenate strings to strings. It should suggest wrapping the integers withstr()before concatenation. Apply that suggestion in the file and save. -
Run the factorial file that produces wrong output. Open and review
bugged_factorial_finder.py. In the same terminal run:Windows:
py bugged_factorial_finder.pymacOS or Linux:
python bugged_factorial_finder.pyThe program should print a value much higher than
720, which is the correct value of 6 factorial. -
Ask Copilot why the output is too high. Send:
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 multipliesfactorialby bothiand an extrafactorialeach loop iteration. Apply the suggested fix (remove the extrafactorialfrom the equation, or change*=to=as Copilot describes) and save. -
Reuse the same prompts on your own project. For a crash:
Explain in depth why my code produces the following error and how I can fix it:YOUR-ERROR-MESSAGEFor 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?
| Check | Expected signal | If it fails |
|---|---|---|
| Clone | Both bugged_*.py files visible in VS Code | Re-run the VS Code clone dialog |
| Dice before fix | Terminal ends with the TypeError above | Confirm you ran the bugged file, not a fixed copy |
| Dice after fix | Script exits without TypeError and prints a Player result line | Re-read Copilot's str() suggestion and save the file |
| Factorial before fix | Printed value is far above 720 | Confirm you ran bugged_factorial_finder.py |
| Factorial after fix | Printed value is 720 | Re-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 foundon macOS/Linux: Cause: onlypython3is installed. Fix: runpython3 bugged_dice_battle.py(and the factorial file the same way), or install Python from python.org sopythonexists.- Copilot answer is vague: Cause: you omitted the error text or the expected value. Fix: paste the full
TypeErrorline, or say you expected720and 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.