{"meta":{"title":"Learning to debug with GitHub Copilot","intro":"Identify and fix errors in your code by asking GitHub Copilot for help.","product":"Get started","breadcrumbs":[{"href":"/en/get-started","title":"Get started"},{"href":"/en/get-started/learning-to-code","title":"Learn to code"},{"href":"/en/get-started/learning-to-code/learning-to-debug-with-github-copilot","title":"Debug with Copilot"}],"documentType":"article"},"body":"# Learning to debug with GitHub Copilot\n\nIdentify and fix errors in your code by asking GitHub Copilot for help.\n\nFinding 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.\n\n## Prerequisites\n\nThe 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:\n\n* Complete [Set up Visual Studio Code with Copilot](https://code.visualstudio.com/docs/copilot/setup-simplified) in the Visual Studio Code documentation.\n* [Download Python](https://www.python.org/downloads/).\n* Install the [Python extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python).\n\n## Learning to debug through examples\n\nThere are two main situations you'll encounter when you try to run bugged code:\n\n* Your code exits before it finishes running, and you receive an error message.\n* Your code runs without errors, but the output is different from what you expected.\n\nThankfully, Copilot can help debug your code in both situations. To learn how, work through the following examples.\n\n### Debugging an error with GitHub Copilot\n\nWhen 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.\n\nLet's try this out with an example repository: [`new2code/debug-with-copilot`](https://github.com/new2code/debug-with-copilot).\n\n#### Cloning the example repository\n\nFirst, we need to create a local copy of the repository:\n\n1. [Start cloning the new2code/debug-with-copilot repository](vscode://vscode.git/clone?url=https://github.com/new2code/debug-with-copilot) in VS Code. <!-- markdownlint-disable-line GHD003 -->\n2. Choose a location to save the repository on your computer, then click **Select as Repository Destination**.\n3. When prompted, open the repository.\n\n#### Running the bugged file\n\nNow, let's run the [`bugged_dice_battle.py`](https://github.com/new2code/debug-with-copilot/blob/main/bugged_dice_battle.py) file. This program simulates a dice battle between two players.\n\n1. In VS Code, open and review the `bugged_dice_battle.py` file.\n\n2. Open the Command Palette by pressing <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Windows/Linux) or <kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Mac).\n\n3. Type `Terminal: Create New Terminal` and press <kbd>Enter</kbd>.\n\n4. In the terminal tab, paste the following command.\n\n   Windows:\n\n   ```shell copy\n   py bugged_dice_battle.py\n   ```\n\n   Mac or Linux:\n\n   ```shell copy\n   python bugged_dice_battle.py\n   ```\n\n5. Press <kbd>Enter</kbd> to run the program.\n\nUnfortunately, we get some error text in our terminal ending with the following message:\n\n> TypeError: can only concatenate str (not \"int\") to str\n\n#### Debugging the file\n\nTo understand what this error means, [open Copilot Chat in VS Code](vscode://GitHub.Copilot-Chat?ref_product=copilot\\&ref_type=engagement\\&ref_style=text), then paste and send the following prompt: <!-- markdownlint-disable-line GHD003 -->\n\n```text copy\nExplain in depth why my code produces the following error and how I can fix it:\n\nTypeError: can only concatenate str (not \"int\") to str\n```\n\nCopilot 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.\n\nIt will also 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. Practice the final step of debugging by applying Copilot's suggestion to the file.\n\n### Debugging an incorrect output with GitHub Copilot\n\nSometimes, 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.\n\nFor these \"invisible\" bugs, Copilot is particularly useful. Let's get some hands-on experience with the other file in our example repository: `bugged_factorial_finder.py`. It's a Python program that's supposed to calculate a factorial.\n\n#### Running the bugged file\n\nFirst, let's run the program to see the incorrect output:\n\n1. Open and review the `bugged_factorial_finder.py` file.\n\n2. In the terminal you created earlier, paste the following command.\n   Windows:\n\n   ```shell copy\n   py bugged_factorial_finder.py\n   ```\n\n   Mac or Linux:\n\n   ```shell copy\n   python bugged_factorial_finder.py\n   ```\n\n3. Press <kbd>Enter</kbd> to run the program.\n\nUnfortunately, 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.\n\n#### Debugging the file\n\nTo understand what went wrong, [open Copilot Chat](vscode://GitHub.Copilot-Chat?ref_product=copilot\\&ref_type=engagement\\&ref_style=text) and send the following prompt: <!-- markdownlint-disable-line GHD003 -->\n\n```text copy\nWhy is the output of this code so much higher than expected? Please explain in depth and suggest a solution.\n```\n\nCopilot 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.\n\nTo fix this error, Copilot will suggest code that removes the extra `factorial` from the equation, or that changes the `*=` operator to `=`. Make that change now!\n\n## Debugging your own project\n\nNow 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.\n\nFor example, to debug an error message generated by your code, send Copilot the following prompt:\n\n```text copy\nExplain in depth why my code produces the following error and how I can fix it:\n\nYOUR-ERROR-MESSAGE\n```\n\nOtherwise, 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.\n\nWith these tactics, you're well equipped to start squashing bugs in your project!\n\n## Next steps\n\nAs 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 [Debug errors](/en/copilot/copilot-chat-cookbook/debugging-errors)."}