Finding and fixing bugs in code can be frustrating, especially when you're a new developer. Thankfully, tools like GitHub Copilot can quickly identify and squash bugs, letting you focus on more creative, interesting work.
Prerequisites
The examples in this article assume you're using GitHub Copilot to debug a Python project in Visual Studio Code (VS Code). To follow the examples, you need to:
- Complete Set up Visual Studio Code with Copilot in the Visual Studio Code documentation.
- Download Python.
- Install the Python extension for Visual Studio Code.
Learning to debug through examples
There are two main situations you'll encounter when you try to run bugged code:
- Your code exits before it finishes running, and you receive an error message.
- Your code runs without errors, but the output is different from what you expected.
Thankfully, Copilot can help debug your code in both situations. To learn how, work through the following examples.
Debugging an error with GitHub Copilot
When you run bugged code, you'll often receive an error message. The message tells you the file and line where the error occurred and briefly describes what went wrong. However, error messages can be confusing. To fully understand and fix the bug, we can ask Copilot for help.
Let's try this out with the bugged_dice_battle.py
file in the new2code/debug-with-copilot
repository. This program simulates a dice battle between two players using the following code:
# Import the random module to easily generate pseudo-random numbers
import random
# Define a function that simulates a dice battle between two players
def dice_battle():
# Generate random numbers between 1 and 6 for each player's die roll
die_1 = random.randint(1, 6)
die_2 = random.randint(1, 6)
# Compare the die rolls and return the result as a string
if die_1 > die_2:
return "Player 1 rolled a " + die_1 + " and Player 2 rolled a " + die_2 + ". Player 1 wins!"
elif die_1 < die_2:
return "Player 1 rolled a " + die_1 + " and Player 2 rolled a " + die_2 + ". Player 2 wins!"
else:
return "Player 1 rolled a " + die_1 + " and Player 2 rolled a " + die_2 + ". It's a tie!"
print(dice_battle())
First, we need to clone the repository locally:
-
In VS Code, open the Command Palette by pressing Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows/Linux).
-
Type
Git: Clone
and press Enter. -
Paste the URL of the
new2code/debug-with-copilot
repository:Text https://github.com/new2code/debug-with-copilot
https://github.com/new2code/debug-with-copilot
-
Press Enter, then choose a location to save the repository on your computer.
-
When prompted, open the repository in VS Code.
Now that we've cloned the repository, let's run bugged_dice_battle.py
to see the output:
-
Open the Command Palette by pressing Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows/Linux).
-
Type
Terminal: Create New Terminal
and press Enter. -
If you are using Mac or Linux, in the terminal tab, paste the following code:
Shell python bugged_dice_battle.py
python bugged_dice_battle.py
Otherwise, if you are using Windows, paste the following code:
Shell py bugged_dice_battle.py
py bugged_dice_battle.py
-
Press Enter to run the program.
Unfortunately, we get some error text in our terminal ending with the following message: TypeError: can only concatenate str (not "int") to str
. To understand what this means, press Command+Shift+I (Mac) or Ctrl+Alt+I (Windows/Linux) to open Copilot Chat, then paste and send the following prompt:
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
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 will respond that the error occurs because we are trying to concatenate the integers die_1
and die_2
to strings, and you can only concatenate strings to strings. It will then provide an updated version of our code that fixes the bug by using the str()
function to convert the integers to strings before concatenating them.
Debugging an incorrect output with GitHub Copilot
Sometimes, bugged code runs without throwing any errors, but the output is clearly incorrect. In this case, debugging can be more difficult because VS Code can't tell you the location or description of the bug.
For these "invisible" bugs, Copilot is particularly useful. Let's get some hands-on experience using the bugged_factorial_finder.py
file in the new2code/debug-with-copilot
repository. The Python program is supposed to calculate a factorial, and it contains the following code:
# Initialize the factorial result to 1
factorial = 1
# Initialize the input number to 6
number = 6
# Loop from 1 to number (inclusive) and multiply factorial by each number
for i in range(1, number + 1):
factorial *= factorial * i
print(f"The factorial of {number} is {factorial}")
Since we've already cloned the repository locally, let's run bugged_factorial_finder.py
to see the output:
-
In VS Code, open the Command Palette by pressing Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows/Linux).
-
Type
Terminal: Create New Terminal
and press Enter. -
If you are using Mac or Linux, in the terminal tab, paste the following code:
Shell python bugged_factorial_finder.py
python bugged_factorial_finder.py
Otherwise, if you are using Windows, paste the following code:
Shell py bugged_factorial_finder.py
py bugged_factorial_finder.py
-
Press Enter to run the program.
Unfortunately, the code isn't working as expected. We want it to return 720
, the correct value of 6 factorial, but the output is much higher than that.
To understand what went wrong, with the bugged_factorial_finder.py
file open in VS Code, open Copilot Chat and send the following prompt:
Why is the output of this code so much higher than expected? Please explain in depth and suggest a solution.
Why is the output of this code so much higher than expected? Please explain in depth and suggest a solution.
Copilot will point out that, because we're using the *=
operator, we're actually multiplying factorial
by both i
and factorial
. In other words, we're multiplying by an extra factorial
for each iteration of the loop.
To fix this error, Copilot will suggest code that removes the extra factorial
from the equation, or that changes the *=
operator to =
.
Debugging your own project
Now that you've practiced debugging some simple programs with Copilot, you can use the same methodologies to find and fix bugs hiding in your own work.
For example, to debug an error message generated by your code, send Copilot the following prompt:
Explain in depth why my code produces the following error and how I can fix it: YOUR-ERROR-MESSAGE
Explain in depth why my code produces the following error and how I can fix it:
YOUR-ERROR-MESSAGE
Otherwise, if you're debugging an incorrect output, ask Copilot why the output is incorrect and how you can fix it. For the best results, provide as much context as possible on how the output differs from your expectations.
With these tactics, you're well equipped to start squashing bugs in your project!
Next steps
As you continue coding, you'll likely encounter specific problem scenarios and errors that are difficult to debug. For a list of potential issues and example Copilot Chat prompts to fix them, see Depurando erros.