Skip to main content

学习使用 GitHub Copilot 进行调试

通过向 GitHub Copilot 寻求帮助,识别并修复代码中的错误。

查找并修复代码中的 bug 可能会令人沮丧(尤其是在你是新开发人员时)。 值得庆幸的是,GitHub Copilot 等工具可以快速识别和消除 bug,让你可以专注于更具创造性、更有趣的工作。

先决条件

本文中的示例假定你使用 GitHub Copilot 调试 Visual Studio Code (VS Code) 中的 Python 项目。 若要按照这些示例操作,需要:

学习通过示例进行调试

尝试运行有 bug 的代码时,你会遇到两个主要情况:

  • 代码在完成运行之前退出,并且你会收到一条错误消息。
  • 代码运行时没有错误,但是输出与你预期的不同。

值得庆幸的是,Copilot 可以帮助在这两种情况下调试代码。 若要了解如何操作,请完成以下示例。

使用 GitHub Copilot 调试错误

运行有 bug 的代码时,你通常会收到一条错误消息。 该消息告知你发生错误的文件和行,并简要描述问题所在。 但是,错误消息可能会令人困惑。 若要充分理解并修复 bug,我们可以向 Copilot 寻求帮助。

让我们使用 new2code/debug-with-copilot 存储库中的 bugged_dice_battle.py 文件尝试一下。 此程序使用以下代码模拟两名玩家之间的骰子大战:

# 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())

首先,我们需要在本地克隆存储库:

  1. 在 VS Code 中,按 Cmd+Shift+P (Mac) 或 Ctrl+Shift+P (Windows/Linux) 打开命令面板。

  2. 键入 Git: Clone,然后按“Enter”

  3. 粘贴 new2code/debug-with-copilot 存储库的 URL:

    Text
    https://github.com/new2code/debug-with-copilot
    
  4. Enter,然后在计算机上选择保存存储库的位置。

  5. 出现提示时,在 VS Code 中打开存储库。

克隆存储库后,让我们运行 bugged_dice_battle.py 以查看输出:

  1. Cmd+Shift+P (Mac) 或 Ctrl+Shift+P (Windows/Linux) 打开命令面板。

  2. 键入 Terminal: Create New Terminal,然后按“Enter”

  3. 如果你使用的是 Mac 或 Linux,则在终端选项卡中粘贴以下代码:

    Shell
    python bugged_dice_battle.py
    

    除此之外,如果你使用的是 Windows,请粘贴以下代码:

    Shell
    py bugged_dice_battle.py
    
  4. Enter 运行程序。

遗憾的是,我们在终端中得到一些错误文本,以下面的消息结尾:TypeError: can only concatenate str (not "int") to str。 若要理解这是什么意思,请按 Command+Shift+I (Mac) 或 Ctrl+Alt+I (Windows/Linux) 打开 Copilot Chat,然后粘贴并发送以下提示:

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 将响应出现错误,因为我们正尝试将整数 die_1die_2 与字符串连接,而你只能将字符串与字符串连接。 然后,它将提供更新的代码版本,该版本通过使用 str() 函数将整数转换为字符串,然后再将它们连接,从而修复 bug。

使用 GitHub Copilot 调试不正确的输出

有时,有 bug 的代码运行时不会引发任何错误,但输出显然不正确。 在这种情况下,调试可能更加困难,因为 VS Code 无法告知你 bug 的位置或说明。

对于这些“不可见的”bug,Copilot 特别有用。 让我们使用 new2code/debug-with-copilot 存储库中的 bugged_factorial_finder.py 文件获得一些实践经验。 Python 程序应计算阶乘,并包含以下代码:

# 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}")

由于我们已在本地克隆存储库,因此让我们运行 bugged_factorial_finder.py 以查看输出:

  1. 在 VS Code 中,按 Cmd+Shift+P (Mac) 或 Ctrl+Shift+P (Windows/Linux) 打开命令面板。

  2. 键入 Terminal: Create New Terminal,然后按“Enter”

  3. 如果你使用的是 Mac 或 Linux,则在终端选项卡中粘贴以下代码:

    Shell
    python bugged_factorial_finder.py
    

    除此之外,如果你使用的是 Windows,请粘贴以下代码:

    Shell
    py bugged_factorial_finder.py
    
  4. Enter 运行程序。

遗憾的是,代码未按预期工作。 我们希望它返回 720,即 6 阶乘的正确值,但输出远高于此。

若要理解问题所在,在 VS Code 中打开 bugged_factorial_finder.py 文件的情况下,打开 Copilot Chat 并发送以下提示:

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

Copilot 将指出,由于我们使用的是 *= 运算符,因此我们实际上是将 factorial 乘以 ifactorial****。 换句话说,对于循环的每个迭代,我们都会乘以额外的 factorial

若要修复此错误,Copilot 将建议从公式中删除额外 factorial 的代码或将 *= 运算符更改为 = 的代码。

调试自己的项目

练习使用 Copilot 调试一些简单程序后,你可以使用相同的方法查找并修复隐藏在自己的工作中的 bug。

例如,若要调试代码生成的错误消息,请向 Copilot 发送以下提示:

Text
Explain in depth why my code produces the following error and how I can fix it:

YOUR-ERROR-MESSAGE

除此之外,如果你要调试不正确的输出,请询问 Copilot 为什么输出不正确以及如何修复它。 为了获得最佳结果,请提供尽可能多的上下文,说明输出与你的期望有何不同。

借助这些策略,你完全有能力开始清除项目中的 bug 了!

后续步骤

随着你继续编码,你可能会遇到难以调试的特定问题场景和错误。 有关潜在问题的列表以及修复这些问题的示例 Copilot Chat 提示,请参阅“调试错误”。