You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/chapter-2/203.md
+1-6Lines changed: 1 addition & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -24,11 +24,7 @@ We highlighted the Copilot logo in the bottom right of figure 2.1 as you should
24
24
25
25
### 2.3.1 Set up your working folder
26
26
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.
32
28
33
29
> **File not found or file missing errors**
34
30
>
@@ -42,7 +38,6 @@ Let’s check to see if we’ve set up everything properly and that Copilot is w
42
38
43
39

44
40
45
-
46
41
47
42
> Figure 2.3 Select to create the New File as a Python File.
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.
10
10
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:
16
12
17
13
1. Write a prompt to Copilot using comments (`#`) or docstrings (`"""`).
18
14
@@ -42,14 +38,12 @@ To download the dataset, you will have to sign up for a Kaggle account. If you d
42
38
> Figure 2.4 The first few columns and rows of the `nfl_offensive_stats.csv` dataset.
43
39
44
40

45
-
46
41
47
42
48
43
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.
49
44
50
45
Good news: Python has a bunch of tools for reading in CSV files.
51
46
52
-
53
47
54
48
#### Step 1: How many passing yards did Aaron Rodgers throw in 2019-2022
55
49
@@ -99,11 +93,7 @@ yards from column 8 where the 4th column value is
99
93
"""
100
94
```
101
95
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.
107
97
108
98
```python
109
99
passing_yards =0
@@ -307,7 +297,6 @@ When you run this command, you’ll see that a bunch of modules are installed, i
307
297
308
298
> Figure 2.5 The plot produced by the code in listing 2.2.
Copy file name to clipboardExpand all lines: content/chapter-3/301.md
+1-8Lines changed: 1 addition & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -12,17 +12,12 @@ Suppose that you’ve found a word search puzzle in the newspaper that you’d l
12
12
13
13
> Figure 3.1 Example Wordsearch puzzle
14
14
15
-
16
15
17
16

18
17
19
18
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.
20
19
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?
26
21
27
22
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.
28
23
@@ -69,8 +64,6 @@ There’s an alternate way to prompt Copilot to write the code for a function th
69
64
70
65
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.
71
66
72
-
73
-
74
67
Here’s what the alternate approach would look like when writing that same `larger` function:
Copy file name to clipboardExpand all lines: content/chapter-3/304.md
+1-2Lines changed: 1 addition & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,6 @@ There’s no clear rule for what makes a good function, but there are some intui
12
12
13
13
Here are some guidelines that we believe will help you see what makes a good function:
14
14
15
-
16
15
17
16
***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.
18
17
@@ -31,7 +30,7 @@ Here are examples of good leaf functions:
31
30
*_Compute the volume of a sphere_ —- given the sphere’s radius, return its volume.
32
31
33
32
*_Find the largest number in a list_ —- given a list, return the largest value.
34
-
33
+
35
34
*_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.
36
35
37
36
*_Print the state of the checkers game_ —- given a 2D list representing the game board, output the game board to the screen in text.
Copy file name to clipboardExpand all lines: content/chapter-3/306.md
+5-29Lines changed: 5 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -56,19 +56,14 @@ This way, we can call the function as many times as we like without going throug
56
56
57
57
> Figure 3.4 Running Python in an interactive session in the Terminal of VSCode. Note the \>>> at the bottom of the Terminal.
58
58
59
-
60
59
61
60

62
61
63
62
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.
64
63
65
64
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`.
66
65
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.
72
67
73
68
```
74
69
>>> 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
89
84
90
85
91
86
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.
97
88
98
89
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.
99
90
@@ -123,7 +114,6 @@ Here’s an equivalent way to write the code for our `money_made` function:
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.
129
119
@@ -190,11 +180,7 @@ Let’s try one more:
190
180
True
191
181
```
192
182
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.
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.
371
353
372
354
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:
373
355
@@ -457,8 +439,6 @@ Let’s try a couple more. In each case, calculate by hand what you expect the a
457
439
458
440
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.
459
441
460
-
461
-
462
442
For example, here’s another solution for num\_points that we got from the other Copilot suggestions.
463
443
464
444
```python
@@ -531,10 +511,6 @@ But, as usual, you shouldn’t be content with just one test case. This function
531
511
'zap'
532
512
```
533
513
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!
539
515
540
516
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.
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.
10
10
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.
16
12
17
13
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."
18
14
@@ -41,8 +37,6 @@ Return the word worth the most points.
41
37
42
38
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!).
43
39
44
-
45
-
46
40
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.
47
41
48
42
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.
Copy file name to clipboardExpand all lines: content/chapter-4/402.md
-7Lines changed: 0 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -12,15 +12,12 @@ We suspect that, soon, the Copilot Labs extension, or parts of it, will be folde
12
12
13
13
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.
14
14
15
-
16
15
17
16
> Figure 4.1 The Copilot Labs view in VSCode.
18
17
19
-
20
18
21
19

22
20
23
-
24
21
25
22
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.
26
23
@@ -36,18 +33,14 @@ After highlighting the code, you should now see the code appear in the left in t
36
33
37
34
> Figure 4.3 The code from the best\_word function appearing in Copilot Labs.
38
35
39
-
40
36
41
37

42
38
43
-
44
39
45
40
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.
46
41
47
42
> Figure 4.4 The different options for explaining your code in Copilot Labs.
48
43
49
-
50
-
51
44

52
45
53
46
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