Skip to content

Commit 5297f37

Browse files
committed
fix: text style
1 parent d51715d commit 5297f37

File tree

12 files changed

+619
-685
lines changed

12 files changed

+619
-685
lines changed

content/chapter-1/108.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: 10
33
title: "[译] [108] 本章小结"
44
---
55

6-
## 1.8 **Summary**
6+
## 1.8 Summary
77
## 1.8 本章小结
88

99
* Copilot is an AI Assistant, which is an Artificial Intelligence (AI) agent that helps you get work done.

content/chapter-2/230.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "[译] [XXX] XXXXXXXXXXXXXXXX"
44
---
55

66

7-
## 2.3 **Working with Copilot in Visual Studio Code**
7+
## 2.3 Working with Copilot in Visual Studio Code
88

99
Now that you have your system set up, let’s get acquainted with the VSCode interface shown in figure 2.1. (You may need to click on the “Explorer” icon in the middle/top left to have this same view.) The following regions are identified in figure 2.1:
1010

@@ -22,21 +22,21 @@ We highlighted the Copilot logo in the bottom right of figure 2.1 as you should
2222
2323
![](chapter-2.files/chapter-29250.png)
2424

25-
#### **Set up your working folder**
25+
### 2.3.1 Set up your working folder
2626

2727
In the top of the Activity Bar on the left in VSCode you will find the “Explorer” as the top icon. After you click on the “Explorer”, it should say
2828

2929

3030

3131
“No Folder Open.” Click on the button to “Open Folder” and select a folder in your computer (or make a new one—we like the folder name fun\_with\_Copilot). Once you’ve opened this folder, your workspace will be the folder you opened which means you should have your code and any data files, like the one we’ll use later this chapter, in that folder.
3232

33-
**File not found or file missing errors**
33+
> **File not found or file missing errors**
34+
>
35+
> If you ever receive an error that says you are missing a file, these are the kind of errors that can be really annoying when writing software. It could be that you just didn’t put the file in your working folder, this happens. That’s an easy fix by copying or moving the file into the correct folder. However, sometimes, you’ll look in the folder and the file will be there, but when you run your code in VSCode, Python can’t seem to find it. If this happens to you (it happened to us when writing the book!), be sure to have the folder with the code and the desired file open using Explorer in VSCode (as shown in the Side Bar in figure 2.1).
3436
35-
If you ever receive an error that says you are missing a file, these are the kind of errors that can be really annoying when writing software. It could be that you just didn’t put the file in your working folder, this happens. That’s an easy fix by copying or moving the file into the correct folder. However, sometimes, you’ll look in the folder and the file will be there, but when you run your code in VSCode, Python can’t seem to find it. If this happens to you (it happened to us when writing the book!), be sure to have the folder with the code and the desired file open using Explorer in VSCode (as shown in the Side Bar in figure 2.1).
37+
### 2.3.2 Check to see if your setup is working properly
3638

37-
#### **Check to see if your setup is working properly**
38-
39-
Let’s check to see if we’ve set up everything properly and that Copilot is working. To do this, start by creating a new file to hold our program. You do this by going to “File->New File” (figure 2.2), then selecting “Python File” (figure 2.3).
39+
Let’s check to see if we’ve set up everything properly and that Copilot is working. To do this, start by creating a new file to hold our program. You do this by going to “File -> New File” (figure 2.2), then selecting “Python File” (figure 2.3).
4040

4141
> Figure 2.2 How to create a new file in VSCode.
4242
@@ -65,43 +65,51 @@ To trigger Copilot to start giving us code (or more comments), press Enter at th
6565
Here’s what happened for us:
6666

6767
```python
68-
# output "Hello Copilot" to the screen print("Hello Copilot")
68+
# output "Hello Copilot" to the screen
69+
print("Hello Copilot")
6970
```
7071

7172

72-
If you still do not see a suggestion from Copilot, try pressing <CTRL>+Enter (hold CTRL” while pressing Enter”). When you press <Ctrl>+Enter, a new window on the right-hand side of the screen should appear. The window will be to the right of your editor window with the program and will be called “GitHub Copilot”. If that window does not appear, there may be something wrong with your setup and we encourage you to go to the Appendix to double check that you followed all the steps correctly or go to our book website to find (or ask for) help.
73+
If you still do not see a suggestion from Copilot, try pressing `<CTRL>+Enter` (hold CTRL” while pressing Enter”). When you press `<Ctrl>+Enter`, a new window on the right-hand side of the screen should appear. The window will be to the right of your editor window with the program and will be called “GitHub Copilot”. If that window does not appear, there may be something wrong with your setup, and we encourage you to go to the Appendix to double-check that you followed all the steps correctly or go to our book website to find (or ask for) help.
7374

74-
If you saw the suggestion, like above, from Copilot, then you need to just press <Tab> to accept Copilot’s suggestion. Once you do this, the suggestion that was previously in light gray italics should now be in a standard font like below:
75+
If you saw the suggestion, like above, from Copilot, then you need to just press `<Tab>` to accept Copilot’s suggestion. Once you do this, the suggestion that was previously in light gray italics should now be in a standard font like below:
7576

7677
```python
77-
# output "Hello Copilot" to the screen #A print("Hello Copilot") #B
78+
# output "Hello Copilot" to the screen #A
79+
print("Hello Copilot") #B
7880
```
7981

8082

81-
If you are seeing different code than this, it’s because of something we mentioned in the introduction: Copilot is non-deterministic so you may see different code than us. We mention this because sometimes Copilot makes a minor mistake with the code here and may give you code similar to this:
83+
If you are seeing different code than this, it’s because of something we mentioned in the introduction: Copilot is non-deterministic, so you may see different code than us. We mention this because sometimes Copilot makes a minor mistake with the code here and may give you code similar to this:
84+
85+
```python
86+
print "Hello Copilot"
87+
```
8288

83-
print “Hello Copilot"
8489

8590
You might think this slight difference (no parentheses around “Hello Copilot”) wouldn’t matter, but it does. Before Python 3, this was the correct syntax for a print statement and when Python 3 was introduced, it switched to the code with parentheses. Since we’re running Python 3, you need to have the parentheses for the code to work. You might ask why Copilot gets this wrong, but the issue is Copilot was trained on Python code before Python 3.
8691

8792
If this seems annoying, we agree. But it’s a hint of the frustration novice programmers went through before Copilot. Most of what Copilot suggests is syntactically correct. But if you were a novice writing the code from scratch, missing parentheses or a missing colon somewhere might cost you a lot of time.
8893

8994
Now that we have the correct code:
9095

91-
# output "Hello Copilot" to the screen print("Hello Copilot")
96+
```python
97+
# output "Hello Copilot" to the screen
98+
print("Hello Copilot")
99+
```
92100

93101
which, as you might guess, prints “Hello Copilot” to the screen, we should test it. First, you’ll want to save your file by going to File->Save.
94102

95-
**Be sure to save your file before you run it**
103+
> **Be sure to save your file before you run it**. We’re embarrassed to admit the amount of time we’ve spent trying to fix code that was actually correct but hadn’t been saved.
96104
97-
Be sure to always save your file before running it. We’re embarrassed to admit the amount of time we’ve spent trying to fix code that was actually correct but hadn’t been saved.
105+
To run your program, go to the top right corner of the text editor and press the “Run Code” icon as shown in figure 2.1. After pressing the icon, in the Terminal section at the bottom, you should see something like this:
98106

99-
107+
```shell
108+
> & C:/Users/YOURNAME/AppData/Local/Programs/Python/Python311/Python.exe c:/Users/YOURNAME/Copilot-book/first_Copilot_program.py
100109

101-
To run your program, go to the top right corner of the text editor and press the “Run Code" icon as shown in figure 2.1. After pressing the icon, in the Terminal section at the bottom, you should see something like this:
102-
103-
\> & C:/Users/YOURNAME/AppData/Local/Programs/Python/Python311/Pyt Hello Copilot
110+
Hello Copilot
111+
```
104112

105-
The top line starting with “>” is the command for the computer to run your code and all it says is to run your first\_Copilot\_program.py using Python. The second line is the output from running the command and it says “Hello Copilot” which is what we’d hoped to see.
113+
The top line starting with “>” is the command for the computer to run your code and all it says is to run your first\_Copilot\_program.py using Python. The second line is the output from running the command, and it says “Hello Copilot” which is what we’d hoped to see.
106114

107-
**Congratulations! You’ve written your first program!** We now know that your programming environment is setup correctly and we can move onto our first programming task. But before we do, we’d like to go over some tips for how to deal with some common issues we’ve encountered when working with Copilot so you have these tips available to you when working through the next example.
115+
**Congratulations! You’ve written your first program!** We now know that your programming environment is set up correctly, and we can move onto our first programming task. But before we do, we’d like to go over some tips for how to deal with some common issues we’ve encountered when working with Copilot, so you have these tips available to you when working through the next example.

content/chapter-2/240.md

Lines changed: 34 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,42 @@ title: "[译] [XXX] XXXXXXXXXXXXXXXX"
44
---
55

66

7-
## 2.4 **Addressing common Copilot challenges**
7+
## 2.4 Addressing common Copilot challenges
88

99
It may seem early in your experience with Copilot to start talking about common challenges with Copilot, but you may have already run into this when writing your first program and you’ll certainly encounter some of these working through our next example and in the next chapter, so we wanted to give these to you now.
1010

1111
In our time working with Copilot, we’ve run into a few common challenges. These challenges will likely decrease with time as Copilot improves, but they were still issues at the time of writing. Although the challenges in table 2.1 are not exhaustive of what you might encounter, we hope our tips on how to address these common challenges will help you get up and running quickly. We’ll keep a running list at our book’s website so please feel free to reach out to us on the forums if you feel we’ve missed something!
1212

13-
**Table 2.1 Common Challenges working with Copilot**
14-
15-
16-
17-
Challenge
18-
19-
Description
20-
21-
Remedies
22-
23-
1\. Add a newline (Enter) between your comment and Copilot’s suggestion to help it switch from comments to code.
24-
25-
If you give Copilot a prompt using the comment symbol (#), when you start a new line, it wants to just give you more comments rather than code. For example:
26-
27-
2\. If a newline doesn’t work, you can type a letter or two of code (no comment symbol). A couple letters from a keyword in your prompt usually works. For example:
28-
29-
**# output "Hello Copilot" to the screen**
30-
31-
Comments only
32-
33-
**# output "Hello Copilot" to the screen**
34-
35-
# print “Hello world to the screen
36-
37-
We've seen Copilot generate line after line of comments, sometimes repeating itself! When this happens, suggestion #3 (use docstrings) is sometimes the most effective.
38-
39-
pr
40-
41-
A couple letters from a keyword typically causes Copilot to give a code suggestion
42-
43-
3. Switch from using \# comments to docstring comments like this:
44-
45-
_"""_
46-
47-
_output_ _"Hello_ _Copilot" to the screen_
48-
49-
50-
51-
_"""_
52-
53-
4. Use <Ctrl>+Enter to see if Copilot will give you suggestions that are code rather than comments
54-
55-
**Table 2.1 Common Challenges working with Copilot (continued)**
56-
57-
Challenge
58-
59-
Description
60-
61-
Remedies
62-
63-
Much of this book is about how to address this issue, but here are some quick tips to get Copilot to help.
64-
65-
1. Change your prompt to see if you can describe what you need better.
66-
67-
Wrong Code
68-
69-
Sometimes Copilot just gives you obviously wrong code from the start. (You’ll learn throughout this book how to identify incorrect code!)
70-
71-
In addition, sometimes Copilot seems to get stuck down wrong paths. For example, it might seem to be trying to solve a different problem than
72-
73-
2. Try using <Ctrl> + Enter to find a suggestion from Copilot that is the correct code.
74-
75-
3. Close the VSCode program, wait a little bit, and restart it. This can help clear the Copilot cache to get new suggestions.
76-
77-
4. Try breaking down the problem into smaller steps
78-
79-
80-
81-
what you’ve asked it to solve. (Suggestion 3, in particular, can help with getting Copilot to go down a new path.)
82-
83-
(see Chapter 7 for more details).
84-
85-
5. Debug the code (see Chapter 8).
86-
87-
6. Try asking ChatGPT for the code and feed its suggestions into VSCode. A different LLM can sometimes give suggestions that help the other LLM to get unstuck.
88-
89-
**Table 2.1 Common Challenges working with Copilot (continued)**
90-
91-
Challenge
92-
93-
Description
94-
95-
Remedies
96-
97-
Copilot gives you:
98-
99-
We’ve had Copilot seem to tell us to write our own code by generating
100-
101-
We believe this is happening when we ask Copilot to solve a problem that has been given by an instructor to students to solve in the past. Why?
102-
103-
Well, when we write our assignments for our students, we (as instructors) often write some code and then tell our students to write the rest by writing:
104-
105-
# YOUR CODE HERE
106-
107-
108-
109-
# YOUR CODE HERE
110-
111-
this (or similar text) after a prompt:
112-
113-
# YOUR CODE HERE
114-
115-
Where we want students to write their code.
116-
117-
Students tend to leave that comment in their solution code which means Copilot was trained to think this comment is an important part of the solution (it’s not). Often, we’re able to solve this problem by finding reasonable solutions in the Copilot suggestions with
118-
119-
<CTRL>+Enter, but please see the solutions for Wrong Code if that doesn’t work.
120-
121-
Missing Modules
122-
123-
Copilot gives you code, but it won’t work because there are modules missing. (Modules are additional libraries that can be added to Python to give pre-built functionality.)
124-
125-
See Section 2.5, under “Modules” for how to install new modules on your machine.
13+
> Table 2.1 Common Challenges working with Copilot
14+
15+
<table id="table001">
16+
<thead>
17+
<tr>
18+
<th> <p>Challenge</p> </th>
19+
<th> <p>Description</p> </th>
20+
<th> <p>Remedies</p> </th>
21+
</tr>
22+
</thead>
23+
<tbody>
24+
<tr>
25+
<td> <p><a id="idIndexMarker036" href="/book/learn-ai-assisted-python-programming/chapter-2/"></a>Comments only</p> </td>
26+
<td> <p>If you give Copilot a prompt using the comment symbol (#), when you start a newline, it wants to just give you more comments rather than code. For example:</p> <p><strong><kbd># output "Hello Copilot" to the screen</kbd></strong></p> <p><kbd># print "Hello world" to the screen</kbd></p> <p>We’ve seen Copilot generate line after line of comments, sometimes repeating itself! When this happens, suggestion 3 (use docstrings) is sometimes the most effective.</p> </td>
27+
<td> <p>1. Add a newline (Enter) between your comment and Copilot’s suggestion to help it switch from comments to code.</p> <p>2. If a newline doesn’t work, you can type a letter or two of code (no comment symbol). A couple letters from a keyword in your prompt usually works. For example:</p> <p><strong><kbd># output "Hello Copilot" to the screen</kbd></strong></p> <p><kbd>pr</kbd></p> <p>A couple letters from a keyword typically causes Copilot to give a code suggestion.</p> <p>3. Switch from using # comments to <a id="idIndexMarker037" href="/book/learn-ai-assisted-python-programming/chapter-2/"></a>docstring comments like this:</p> <p><i><kbd>"""</kbd></i></p> <p><i><kbd>output "Hello Copilot" to the screen</kbd></i></p> <p><i><kbd>"""</kbd></i></p> <p>4. Use Ctrl–Enter to see if Copilot will give you suggestions that are code rather than comments</p> </td>
28+
</tr>
29+
<tr>
30+
<td> <p>Wrong code</p> </td>
31+
<td> <p>Sometimes Copilot just gives you obviously wrong code from the start. (You’ll learn throughout this book how to identify incorrect code!)</p> <p>In addition, sometimes Copilot seems to get stuck down wrong paths. For example, it might seem to be trying to solve a different problem than what you’ve asked it to solve. (Suggestion 3, in particular, can help with getting Copilot to go down a new path.)</p> </td>
32+
<td> <p>Much of this book is about how to address this problem, but here are some quick tips to get Copilot to help:</p> <p>1. Change your prompt to see if you can better describe what you need.</p> <p>2. Try using Ctrl–Enter to find a suggestion from Copilot that is the correct code.</p> <p>3. Close the VS Code program, wait a little bit, and restart it. This can help clear the Copilot cache to get new suggestions.</p> <p>4. Try breaking down the problem into smaller steps (see chapter 7 for more details).</p> <p>5. Debug the code (see chapter 8).</p> <p>6. Try asking ChatGPT for the code and feed its suggestions into VS Code. A different large language model (LLM) can sometimes give suggestions that help the other LLM to get unstuck.</p> </td>
33+
</tr>
34+
<tr>
35+
<td> <p>Copilot gives you</p> <p><kbd># YOUR CODE HERE</kbd></p> </td>
36+
<td> <p>We’ve had Copilot seem to tell us to write our own code by generating this (or similar text) after a prompt:</p> <p><kbd># YOUR CODE HERE</kbd></p> </td>
37+
<td> <p>We believe this is happening when we ask Copilot to solve a problem that has been given by an instructor to students to solve in the past. Why? Well, when we write our assignments for our students, we (as instructors) often write some code and then tell our students to write the rest by writing</p> <p><kbd># YOUR CODE HERE</kbd></p> <p>where we want students to write their code. Students tend to leave that comment in their solution code, which means Copilot was trained to think this comment is an important part of the solution (it’s not). Often, we’re able to solve this problem by finding reasonable solutions in the Copilot suggestions with Ctrl–Enter, but please see the solutions for Wrong Code if that doesn’t work.</p> </td>
38+
</tr>
39+
<tr>
40+
<td> <p>Missing modules</p> </td>
41+
<td> <p>Copilot gives you code, but it won’t work because there are modules missing. (Modules are additional libraries that can be added to Python to give prebuilt functionality.)</p> </td>
42+
<td> <p>See section 2.5, under “Modules” for how to install new modules on your machine.</p> </td>
43+
</tr>
44+
</tbody>
45+
</table>

0 commit comments

Comments
 (0)