{"meta":{"title":"了解如何使用 GitHub Copilot 进行调试","intro":"通过寻求帮助 GitHub Copilot 来识别并修复代码中的错误。","product":"开始","breadcrumbs":[{"href":"/zh/get-started","title":"开始"},{"href":"/zh/get-started/learning-to-code","title":"学习编写代码"},{"href":"/zh/get-started/learning-to-code/learning-to-debug-with-github-copilot","title":"使用 Copilot 进行调试"}],"documentType":"article"},"body":"# 了解如何使用 GitHub Copilot 进行调试\n\n通过寻求帮助 GitHub Copilot 来识别并修复代码中的错误。\n\n查找并修复代码中的 bug 可能会令人沮丧（尤其是在你是新开发人员时）。 值得庆幸的是，诸如 GitHub Copilot 工具可以快速识别和挤压 bug，让你专注于更具创造性、有趣的工作。\n\n## 先决条件\n\n本文中的示例假定你在 （GitHub Copilot） 中调试Visual Studio CodePython项目VS Code。 若要按照这些示例操作，需要：\n\n* 完成文档中[的设置Visual Studio Code。Copilot](https://code.visualstudio.com/docs/copilot/setup-simplified)Visual Studio Code\n* [Download Python](https://www.python.org/downloads/)。\n* 安装[Python扩展。Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python)\n\n## 学习通过示例进行调试\n\n尝试运行有 bug 的代码时，你会遇到两个主要情况：\n\n* 代码在完成运行之前退出，并且你会收到一条错误消息。\n* 代码运行时没有错误，但是输出与你预期的不同。\n\n值得庆幸的是， Copilot 这两种情况下都可以帮助调试代码。 若要了解如何操作，请完成以下示例。\n\n### 使用  调试错误 GitHub Copilot\n\n运行有 bug 的代码时，你通常会收到一条错误消息。 该消息告知你发生错误的文件和行，并简要描述问题所在。 但是，错误消息可能会令人困惑。 为了充分了解和修复 bug，我们可以寻求帮助 Copilot 。\n\n让我们通过一个示例仓库试试看：[`new2code/debug-with-copilot`](https://github.com/new2code/debug-with-copilot)。\n\n#### 克隆示例存储库\n\n首先，需要创建该仓库的本地副本：\n\n1. [开始在  中克隆 new2code/debug-with-copilot 存储库](vscode://vscode.git/clone?url=https://github.com/new2code/debug-with-copilot)。VS Code <!-- markdownlint-disable-line GHD003 -->\n2. \\*\\*\n   \\*\\*选择在计算机上保存存储库的位置，然后单击“选择为存储库目标”。\n3. 出现提示时，打开存储库。\n\n#### 运行存在 bug 的文件\n\n接下来，我们运行 [`bugged_dice_battle.py`](https://github.com/new2code/debug-with-copilot/blob/main/bugged_dice_battle.py) 文件。 此程序模拟两名玩家之间的骰子对战。\n\n1. 在 VS Code中，打开并查看 `bugged_dice_battle.py` 文件。\n\n2. 按<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>（Windows/Linux）或<kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>（Mac）打开命令面板。\n\n3. 输入 `Terminal: Create New Terminal` 并按 <kbd>Enter</kbd> 键。\n\n4. 在终端选项卡中，粘贴以下命令。\n\n   Windows：\n\n   ```shell copy\n   py bugged_dice_battle.py\n   ```\n\n   Mac 或 Linux：\n\n   ```shell copy\n   python bugged_dice_battle.py\n   ```\n\n5. 按 <kbd>Enter</kbd> 运行程序。\n\n遗憾的是，我们的终端显示了一些错误信息，并出现以下消息：\n\n> TypeError：只能将 str（而非“int”）连接到 str\n\n#### 调试文件\n\n若要了解此错误的含义， [请打开 Copilot Chat ， VS Code](vscode://GitHub.Copilot-Chat?ref_product=copilot\\&ref_type=engagement\\&ref_style=text)然后粘贴并发送以下提示： <!-- 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 将响应错误，因为我们尝试连接整数 `die_1` 和 `die_2` 字符串，并且只能将字符串连接到字符串。\n\n它还将提供一个我们代码的更新版本，该版本通过使用\\*\\*\\*\\* 函数将整数转换为字符串，然后再进行连接，从而修复bug`str()`。 通过将建议应用于 Copilot文件来练习调试的最后一步。\n\n### 使用  调试错误的输出 GitHub Copilot\n\n有时，有 bug 的代码运行时不会引发任何错误，但输出显然不正确。 在这种情况下，调试可能更加困难，因为 VS Code 无法告诉你 bug 的位置或说明。\n\n对于这些“不可见”bug， Copilot 特别有用。 让我们通过示例仓库中的另一个文件来动手实践一下：`bugged_factorial_finder.py`。 这是一个用于计算阶乘的Python程序。\n\n#### 运行存在 bug 的文件\n\n首先，我们来运行这个程序，看看它输出的错误结果：\n\n1. 打开并查看 `bugged_factorial_finder.py` 文件。\n\n2. 在之前创建的终端中，粘贴以下命令。\n   Windows：\n\n   ```shell copy\n   py bugged_factorial_finder.py\n   ```\n\n   Mac 或 Linux：\n\n   ```shell copy\n   python bugged_factorial_finder.py\n   ```\n\n3. 按 <kbd>Enter</kbd> 运行程序。\n\n遗憾的是，代码未按预期工作。 我们希望它返回 `720`，即 6 阶乘的正确值，但输出远高于此。\n\n#### 调试文件\n\n若要了解出错的原因， [请打开 Copilot Chat](vscode://GitHub.Copilot-Chat?ref_product=copilot\\&ref_type=engagement\\&ref_style=text) 并发送以下提示： <!-- 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将指出，因为我们正在使用`*=`运算符，因此实际上乘以`factorial`两**者。**`factorial``i` 换句话说，对于循环的每个迭代，我们都会乘以额外的 `factorial`。\n\n若要修复此错误， Copilot 将建议从公式中删除额外的 `factorial` 代码，或将 `*=` 运算符更改为 `=`。 立即进行更改！\n\n## 调试自己的项目\n\n现在，你已练习调试一些简单的程序 Copilot，你可以使用相同的方法查找和修复隐藏在你自己的工作中的错误。\n\n例如，若要调试代码生成的错误消息，请发送 Copilot 以下提示：\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\n否则，如果要调试不正确的输出，请询问 Copilot 输出为何不正确，以及如何修复它。 为了获得最佳结果，请提供尽可能多的上下文，说明输出与你的期望有何不同。\n\n借助这些策略，你完全有能力开始清除项目中的 bug 了！\n\n## 后续步骤\n\n随着你继续编码，你可能会遇到难以调试的特定问题场景和错误。 有关潜在问题和示例 Copilot Chat 提示的列表，请参阅 [调试中出现的错误](/zh/copilot/tutorials/copilot-cookbook/debug-errors)。"}