Skip to content

Commit f765c40

Browse files
committed
fix: text style
1 parent 19c8275 commit f765c40

File tree

9 files changed

+14
-88
lines changed

9 files changed

+14
-88
lines changed

content/chapter-2/203.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ We highlighted the Copilot logo in the bottom right of figure 2.1 as you should
2424

2525
### 2.3.1 Set up your working folder
2626

27-
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
28-
29-
30-
31-
“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.
27+
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 “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.
3228

3329
> **File not found or file missing errors**
3430
>
@@ -42,7 +38,6 @@ Let’s check to see if we’ve set up everything properly and that Copilot is w
4238
4339
![](chapter-2.files/chapter-210805.png)
4440

45-
4641

4742
> Figure 2.3 Select to create the New File as a Python File.
4843

content/chapter-2/205.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ title: "[译] [XXX] XXXXXXXXXXXXXXXX"
88

99
The goal of this next section is twofold: 1) for you to see the workflow of interacting with Copilot and 2) for you to gain an appreciation of how powerful Copilot can be by seeing it solve a fairly complicated task fairly easily.
1010

11-
In our next chapter, we’ll talk through the workflow with Copilot in more
12-
13-
14-
15-
detail, but generally speaking, you’ll use the following steps when authoring code with Copilot:
11+
In our next chapter, we’ll talk through the workflow with Copilot in more detail, but generally speaking, you’ll use the following steps when authoring code with Copilot:
1612

1713
1. Write a prompt to Copilot using comments (`#`) or docstrings (`"""`).
1814

@@ -42,14 +38,12 @@ To download the dataset, you will have to sign up for a Kaggle account. If you d
4238
> Figure 2.4 The first few columns and rows of the `nfl_offensive_stats.csv` dataset.
4339
4440
![](chapter-2.files/chapter-223774.png)
45-
4641

4742

4843
The `nfl_offensive_stats.csv` file is something known as a comma separated value text file (see figure 2.4 for a portion of the file). This is a pretty standard format for storing data. It has a header row at the top that explains what’s in every column. The way that we (or a computer) know the boundaries between columns is to use the commas between cells. Also notice that each row is placed on its own line.
4944

5045
Good news: Python has a bunch of tools for reading in CSV files.
5146

52-
5347

5448
#### Step 1: How many passing yards did Aaron Rodgers throw in 2019-2022
5549

@@ -99,11 +93,7 @@ yards from column 8 where the 4th column value is
9993
"""
10094
```
10195

102-
Notice how we tell the computer which columns are for players, and which are for passing yards. That’s to tell the computer how to interpret the data. Also, notice how we say specifically that we only want to sum the yards in
103-
104-
105-
106-
the case that the player’s name is Aaron Rodgers. Again, we’ll teach you how to write prompts like this as we move forward in the book. Given this prompt, Copilot then produced the code below.
96+
Notice how we tell the computer which columns are for players, and which are for passing yards. That’s to tell the computer how to interpret the data. Also, notice how we say specifically that we only want to sum the yards in the case that the player’s name is Aaron Rodgers. Again, we’ll teach you how to write prompts like this as we move forward in the book. Given this prompt, Copilot then produced the code below.
10797

10898
```python
10999
passing_yards = 0
@@ -307,7 +297,6 @@ When you run this command, you’ll see that a bunch of modules are installed, i
307297

308298
> Figure 2.5 The plot produced by the code in listing 2.2.
309299
310-
311300

312301
![](chapter-2.files/chapter-242745.png)
313302

content/chapter-3/301.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ Suppose that you’ve found a word search puzzle in the newspaper that you’d l
1212

1313
> Figure 3.1 Example Wordsearch puzzle
1414
15-
1615

1716
![](chapter-3.files/chapter-33098.png)
1817

1918
At a high level, your task is “find all of the words in the word search”. Unfortunately, that description of the task isn’t helpful on its own. It doesn’t tell us what steps we need to carry out to solve the problem.
2019

21-
Try working on the problem right now for a couple minutes. How did you
22-
23-
24-
25-
start? How did you break down the overall task to make it more achievable?
20+
Try working on the problem right now for a couple minutes. How did you start? How did you break down the overall task to make it more achievable?
2621

2722
One thing you might do is say, “OK, finding every word is a big task, but a smaller task is just finding the first word (CAT). Let me work on that first!” This is an example of taking a large task and breaking it into smaller tasks. To solve the entire puzzle, then, you could repeat that smaller task for each word that you need to find.
2823

@@ -69,8 +64,6 @@ There’s an alternate way to prompt Copilot to write the code for a function th
6964
7065
By writing the header and docstring, you’ll make it easier for Copilot to generate the right code. In the header, you will be the one deciding on the name of the function and will provide the names of each parameter that you want the function to use. After the function header, you’ll provide a docstring which tells Copilot what the function does. Then, just as before, Copilot will generate the code for the function. Because we gave Copilot the function header, it will be able to learn from the header and is less likely to make mistakes.
7166

72-
73-
7467
Here’s what the alternate approach would look like when writing that same `larger` function:
7568

7669
```python

content/chapter-3/303.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ So, we’re back in `funct1`, on the line `print("")`. Printing an empty piece o
7474

7575
> Figure 3.2 Flow of function execution in our example from listing 3.1.
7676
77-
7877

7978
![](chapter-3.files/chapter-323209.png)
8079

content/chapter-3/304.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ There’s no clear rule for what makes a good function, but there are some intui
1212

1313
Here are some guidelines that we believe will help you see what makes a good function:
1414

15-
1615

1716
* **One clear task to perform.** If they are leaf functions, this might be something like, “compute the volume of a sphere”, “find the largest number in a list”, or “check to see if a list contains a specific value.” Non-leaf functions can achieve more broad goals, like “update the game graphics” or “collect and sanitize input from the user”. Non-leaf functions should still have a particular goal in mind but are designed knowing they will likely call other functions to achieve their goal.
1817

@@ -31,7 +30,7 @@ Here are examples of good leaf functions:
3130
* _Compute the volume of a sphere_ —- given the sphere’s radius, return its volume.
3231

3332
* _Find the largest number in a list_ —- given a list, return the largest value.
34-
33+
3534
* _Check to see if a list contains a specific value_ —- given a list and a value, return true if the list contains the value and false if it does not.
3635

3736
* _Print the state of the checkers game_ —- given a 2D list representing the game board, output the game board to the screen in text.

content/chapter-3/306.md

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,14 @@ This way, we can call the function as many times as we like without going throug
5656

5757
> Figure 3.4 Running Python in an interactive session in the Terminal of VSCode. Note the \>>> at the bottom of the Terminal.
5858
59-
6059

6160
![](chapter-3.files/chapter-338016.png)
6261

6362
At the bottom of the resulting window, you will see three greater-than symbols >>>. This is called a _prompt_, and you’re allowed to type Python code here. (This _prompt_ has nothing to do with the kind of prompt that we use when interacting with Copilot.) It will show us the result of the code that we type right away, which is convenient and fast.
6463

6564
To call our `money\_made` function, we need to provide three arguments, and they will be assigned left to right to the parameters. Whatever we put first will be assigned to `num\_shares`, whatever we put second will be assigned to `purchase\_share\_price`, and whatever we put third will be assigned to `current\_share\_price`.
6665

67-
68-
69-
Let’s try this! At the prompt, type the following and press enter (or
70-
71-
<shift>+Enter). Don’t type the >>> as that’s already there, and we are including it throughout the book to make it clear where we are typing. Please see figure 3.5 for an example of running the function in the terminal at the Python prompt.
66+
Let’s try this! At the prompt, type the following and press enter (or `<shift>+Enter`). Don’t type the >>> as that’s already there, and we are including it throughout the book to make it clear where we are typing. Please see figure 3.5 for an example of running the function in the terminal at the Python prompt.
7267

7368
```
7469
>>> money_made(10, 15, 17)
@@ -89,11 +84,7 @@ Is `20` correct? Well, we bought 10 shares, and each of them went up $2 (from $1
8984

9085

9186

92-
We’re not done testing, though. When testing a function, you want to test it in various ways, not just once. All one test case tells you is that it happened to work with the particular input values that you provided. The more test cases
93-
94-
95-
96-
we try, each testing the function in a different way, the more confident we are that our function is correct.
87+
We’re not done testing, though. When testing a function, you want to test it in various ways, not just once. All one test case tells you is that it happened to work with the particular input values that you provided. The more test cases we try, each testing the function in a different way, the more confident we are that our function is correct.
9788

9889
How do we test this function in a different way? We’re looking for inputs that are somehow a different _category_ of input. One not-so-good test right now would be to say, “what if our stock went from $15 to $18, instead of $15 to $17?”. This is pretty much the same test as before, and chances are that it will work just fine.
9990

@@ -123,7 +114,6 @@ Here’s an equivalent way to write the code for our `money_made` function:
123114
price_difference = current_share_price - purchase_share_price
124115
return num_shares * price_difference
125116
```
126-
127117

128118
This may even be easier to read for you: first it figures out the difference in share price, and then it multiplies that by the number of shares. We encourage you to test this version to help convince yourself that it is still correct.
129119

@@ -190,11 +180,7 @@ Let’s try one more:
190180
True
191181
```
192182

193-
Well, technically True is correct, but wow is ‘Leo’ a bad password. We should really have done better with our definition of a strong password.
194-
195-
Let’s change our prompt to give a more reasonable definition of what it means for a password to be strong.
196-
197-
183+
Well, technically True is correct, but wow is ‘Leo’ a bad password. We should really have done better with our definition of a strong password. Let’s change our prompt to give a more reasonable definition of what it means for a password to be strong.
198184

199185
Here's our next prompt and what Copilot gave us.
200186

@@ -363,11 +349,7 @@ password = input("Enter a strong password: ")
363349
```
364350

365351

366-
That `while` keyword creates another kind of loop, this one continuing as long as the entered password is not strong. Copilot is also smart enough to call our earlier `is\_strong\_password` function to determine what counts as a
367-
368-
369-
370-
strong password. As we will see in future chapters, using functions as building blocks in this way is precisely how large programs are built. You will often notice Copilot calling your earlier functions to solve later ones, much as we observed here.
352+
That `while` keyword creates another kind of loop, this one continuing as long as the entered password is not strong. Copilot is also smart enough to call our earlier `is\_strong\_password` function to determine what counts as a strong password. As we will see in future chapters, using functions as building blocks in this way is precisely how large programs are built. You will often notice Copilot calling your earlier functions to solve later ones, much as we observed here.
371353

372354
Let’s test this! Highlight all of our password function code and hit `<Shift>+Enter`. We’ll call the function that we want to test. Then, try typing passwords, hitting enter after each one. You’ll notice that it keeps asking you until you finally provide a strong password:
373355

@@ -457,8 +439,6 @@ Let’s try a couple more. In each case, calculate by hand what you expect the a
457439

458440
There are many ways to write correct code for a function. If you press `<Ctrl>+Enter` and look at the Copilot suggestions, you may see different types of code. It doesn’t necessarily mean that one of these types is right and the others are wrong.
459441

460-
461-
462442
For example, here’s another solution for num\_points that we got from the other Copilot suggestions.
463443

464444
```python
@@ -531,10 +511,6 @@ But, as usual, you shouldn’t be content with just one test case. This function
531511
'zap'
532512
```
533513

534-
However, we wouldn’t test this function on a list that has no words in it. What would it even make sense to do in that case? Regardless of what the function does, it’d be hard to say one way or the other whether it was doing
535-
536-
537-
538-
the correct thing in a situation where there really is no correct behavior!
514+
However, we wouldn’t test this function on a list that has no words in it. What would it even make sense to do in that case? Regardless of what the function does, it’d be hard to say one way or the other whether it was doing the correct thing in a situation where there really is no correct behavior!
539515

540516
Overall, in this chapter we’ve learned about functions in Python and how we can use Copilot to help us write them. We’ve also learned about the characteristics of good functions and how important it is to make sure our functions are solving tasks that can be managed well by Copilot. Our next steps in this book all revolve around understanding if the code produced by Copilot is correct and how to fix it when it isn’t. In the next chapter, we’ll start by learning the basics of being able to read the code produced by Copilot as this gives us the first sanity check for whether Copilot is doing what we think it should be. Then in later chapters we’ll dig deeper into how to carefully test the code and what to do when it is wrong.

content/chapter-4/401.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ title: "[译] [XXX] XXXXXXXXXXXXXXXX"
88

99
When we talk about reading code, what we mean is being able to understand what code does by looking at it. There are two such levels of understanding.
1010

11-
The first is being able to understand, line by line, what a program will do.
12-
13-
14-
15-
This often involves tracing the values of variables as the code runs in order to determine exactly what the code is doing at each step.
11+
The first is being able to understand, line by line, what a program will do. This often involves tracing the values of variables as the code runs in order to determine exactly what the code is doing at each step.
1612

1713
The second is being able to determine the overall purpose of a program. As professors, we often test students at this level with questions that ask them to "explain in plain English."
1814

@@ -41,8 +37,6 @@ Return the word worth the most points.
4137

4238
A **tracing description** of what this program does would be a description of each line. For example, we would say that we're defining a function called `best\_word`. We have a variable called `best\_word` that we start off as a string with no characters, otherwise known as the empty string. (It’s unfortunate that the function and this variable are both called `best\_word`, because it makes it trickier to refer to one or the other, but that’s what Copilot gave us.) We also have another variable `best\_points` that we start at 0. Then we have a for loop over each word in the `word\_list`. Inside the for loop, we call our `num\_points` helper function... and so on. (We’ll be explaining how we know what each line of code does over the next two chapters!).
4339

44-
45-
4640
In contrast, a **description of the overall purpose** would be something like our docstring description: “Return the word with the highest Scrabble point value from a list of words.” Rather than refer to each line, this description refers to the code’s purpose as a whole, explaining what it does at a high level.
4741

4842
You'll come to an overall-purpose level of understanding through a combination of practice with tracing and testing, and we hope you arrive there in full glory by the end of the book. Working at a tracing level generally precedes the ability to work at an overall-purpose level \[1\], so in this chapter and the next, we're going to focus on the tracing level by understanding what each line of code does.

content/chapter-4/402.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ We suspect that, soon, the Copilot Labs extension, or parts of it, will be folde
1212

1313
For now, with the Copilot Labs extension installed, you can highlight some code that you want Copilot to describe to you. Let's try this with our `best\_word` function.
1414

15-
1615

1716
> Figure 4.1 The Copilot Labs view in VSCode.
1817
19-
2018

2119
![](chapter-4.files/chapter-48075.png)
2220

23-
2421

2522
First, click on the Copilot Labs tab in your Activity Bar (on the left-hand side of VSCode) and you should see a window similar to figure 4.1.
2623

@@ -36,18 +33,14 @@ After highlighting the code, you should now see the code appear in the left in t
3633

3734
> Figure 4.3 The code from the best\_word function appearing in Copilot Labs.
3835
39-
4036

4137
![](chapter-4.files/chapter-48572.png)
4238

43-
4439

4540
Figure 4.4 shows the different prompts provided by Copilot that you can use to ask for code explanations. Each will generally yield different responses that vary in how specific they are and whether they produce fewer or more examples. We'll leave it at the default prompt of “Explain Code,” but if you like, you can try other prompts from the drop-down box (shown in figure 4.4). The current options are "Explain Code," "Code Does Following," "Code Does Following (English)," and "Show Example Code." There's also a "Custom" option that allows you to use whatever prompt you like.
4641

4742
> Figure 4.4 The different options for explaining your code in Copilot Labs.
4843
49-
50-
5144
![](chapter-4.files/chapter-48774.png)
5245

5346
The last step is to click Ask Copilot (as shown as the bottom button in figure 4.3). You'll get an explanation of the code. As usual, Copilot is nondeterministic, so your results will likely differ from ours. In fact, if an explanation is confusing you, you might try clicking Ask Copilot again or changing the prompt to get a different explanation. As educators, this is the start of a dream come true, where students will be able to ask for as many explanations as they need to fully understand how code works.

0 commit comments

Comments
 (0)