Skip to content

Commit 07a77c8

Browse files
authored
Merge pull request #37282 from github/repo-sync
Repo sync
2 parents a8c9f94 + 14e9e7a commit 07a77c8

File tree

3 files changed

+62
-84
lines changed

3 files changed

+62
-84
lines changed

content/get-started/learning-to-code/learning-to-debug-with-github-copilot.md

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,21 @@ Thankfully, {% data variables.product.prodname_copilot_short %} can help debug y
3030

3131
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 {% data variables.product.prodname_copilot_short %} for help.
3232

33-
Let's try this out with the [`bugged_dice_battle.py`](https://github.com/new2code/debug-with-copilot/blob/main/bugged_dice_battle.py) file in the [`new2code/debug-with-copilot`](https://github.com/new2code/debug-with-copilot) repository. This program simulates a dice battle between two players using the following code:
33+
Let's try this out with an example repository: [`new2code/debug-with-copilot`](https://github.com/new2code/debug-with-copilot).
3434

35-
```python
36-
# Import the random module to easily generate pseudo-random numbers
37-
import random
35+
#### Cloning the example repository
3836

39-
# Define a function that simulates a dice battle between two players
40-
def dice_battle():
41-
42-
# Generate random numbers between 1 and 6 for each player's die roll
43-
die_1 = random.randint(1, 6)
44-
die_2 = random.randint(1, 6)
45-
46-
# Compare the die rolls and return the result as a string
47-
if die_1 > die_2:
48-
return "Player 1 rolled a " + die_1 + " and Player 2 rolled a " + die_2 + ". Player 1 wins!"
49-
elif die_1 < die_2:
50-
return "Player 1 rolled a " + die_1 + " and Player 2 rolled a " + die_2 + ". Player 2 wins!"
51-
else:
52-
return "Player 1 rolled a " + die_1 + " and Player 2 rolled a " + die_2 + ". It's a tie!"
53-
54-
print(dice_battle())
55-
```
56-
57-
First, we need to create a local copy of the example repository:
37+
First, we need to create a local copy of the repository:
5838

5939
1. [Start cloning the new2code/debug-with-copilot repository](vscode://vscode.git/clone?url=https://github.com/new2code/debug-with-copilot) in {% data variables.product.prodname_vscode_shortname %}. <!-- markdownlint-disable-line GHD003 -->
6040
1. Choose a location to save the repository on your computer, then click **Select as Repository Destination**.
6141
1. When prompted, open the repository.
6242

63-
Now that we've cloned the repository, let's run `bugged_dice_battle.py` to see the output:
43+
#### Running the bugged file
44+
45+
Now, 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.
6446

47+
1. In {% data variables.product.prodname_vscode_shortname %}, open and review the `bugged_dice_battle.py` file.
6548
1. 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).
6649
1. Type `Terminal: Create New Terminal` and press <kbd>Enter</kbd>.
6750
1. In the terminal tab, paste the following command.
@@ -84,38 +67,31 @@ Unfortunately, we get some error text in our terminal ending with the following
8467

8568
> TypeError: can only concatenate str (not "int") to str
8669

87-
To understand what this means, press <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>I</kbd> (Windows/Linux) or <kbd>Command</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd> (Mac) to **open {% data variables.product.prodname_copilot_chat_short %}**, then paste and send the following prompt:
70+
#### Debugging the file
71+
72+
To understand what this error means, press <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>I</kbd> (Windows/Linux) or <kbd>Command</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd> (Mac) to **open {% data variables.product.prodname_copilot_chat_short %}**, then paste and send the following prompt:
8873

8974
```text copy
9075
Explain in depth why my code produces the following error and how I can fix it:
9176
9277
TypeError: can only concatenate str (not "int") to str
9378
```
9479

95-
{% data variables.product.prodname_copilot_short %} 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.
80+
{% data variables.product.prodname_copilot_short %} 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.
81+
82+
It 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 {% data variables.product.prodname_copilot_short %}'s suggestion to the file.
9683
9784
### Debugging an incorrect output with {% data variables.product.prodname_copilot %}
9885
9986
Sometimes, bugged code runs without throwing any errors, but the output is clearly incorrect. In this case, debugging can be more difficult because {% data variables.product.prodname_vscode_shortname %} can't tell you the location or description of the bug.
10087
101-
For these "invisible" bugs, {% data variables.product.prodname_copilot_short %} is particularly useful. Let's get some hands-on experience using the [`bugged_factorial_finder.py`](https://github.com/new2code/debug-with-copilot/blob/main/bugged_factorial_finder.py) file in the [`new2code/debug-with-copilot`](https://github.com/new2code/debug-with-copilot) repository. The Python program is supposed to calculate a factorial, and it contains the following code:
102-
103-
```python
104-
# Initialize the factorial result to 1
105-
factorial = 1
106-
107-
# Initialize the input number to 6
108-
number = 6
88+
For these "invisible" bugs, {% data variables.product.prodname_copilot_short %} 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.
10989
110-
# Loop from 1 to number (inclusive) and multiply factorial by each number
111-
for i in range(1, number + 1):
112-
factorial *= factorial * i
90+
#### Running the bugged file
11391
114-
print(f"The factorial of {number} is {factorial}")
115-
```
116-
117-
Since we've already cloned the repository locally, let's run `bugged_factorial_finder.py` to see the output:
92+
First, let's run the program to see the incorrect output:
11893
94+
1. Open and review the `bugged_factorial_finder.py` file.
11995
1. In the terminal you created earlier, paste the following command.
12096
Windows:
12197
@@ -133,15 +109,17 @@ Since we've already cloned the repository locally, let's run `bugged_factorial_f
133109

134110
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.
135111
136-
To understand what went wrong, with the `bugged_factorial_finder.py` file open in {% data variables.product.prodname_vscode_shortname %}, open {% data variables.product.prodname_copilot_chat_short %} and send the following prompt:
112+
#### Debugging the file
113+
114+
To understand what went wrong, open {% data variables.product.prodname_copilot_chat_short %} and send the following prompt:
137115
138116
```text copy
139117
Why is the output of this code so much higher than expected? Please explain in depth and suggest a solution.
140118
```
141119
142120
{% data variables.product.prodname_copilot_short %} 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.
143121

144-
To fix this error, {% data variables.product.prodname_copilot_short %} will suggest code that removes the extra `factorial` from the equation, or that changes the `*=` operator to `=`.
122+
To fix this error, {% data variables.product.prodname_copilot_short %} will suggest code that removes the extra `factorial` from the equation, or that changes the `*=` operator to `=`. Make that change now!
145123

146124
## Debugging your own project
147125

package-lock.json

Lines changed: 40 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
"mdast-util-to-markdown": "2.1.0",
302302
"mdast-util-to-string": "^4.0.0",
303303
"morgan": "^1.10.0",
304-
"next": "^15.2.3",
304+
"next": "^15.2.4",
305305
"ora": "^8.0.1",
306306
"parse5": "7.1.2",
307307
"quick-lru": "7.0.0",

0 commit comments

Comments
 (0)