-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
270 lines (205 loc) · 10.3 KB
/
index.html
File metadata and controls
270 lines (205 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!doctype html>
<!doctype html>
<head>
<title>CS106A</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../../css/style.css">
<!-- Java Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="../../plugins/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- Python highlighting -->
<script src="../../plugins/prism/prism.js"></script>
<link href="../../plugins/prism/prism.css" rel="stylesheet" />
<!-- Probability Packages -->
<script src="../../plugins/probability/gaussian.js"></script>
<script src="../../plugins/color.js"></script>
<!-- font awesome -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<!-- SWAL -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<!-- Stanford -->
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Source+Serif+Pro:400,600,700' rel='stylesheet' type='text/css'>
<!-- Math Jax -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script src="../../plugins/math.min.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</head>
<body>
<nav id="mainNav" class="navbar navbar-expand-md navbar-dark bg-dark" style="padding-bottom: 0px;">
<div class="container">
<a class="navbar-brand" style="padding-top:0px;color:rgba(255,255,255,.7)" href="../../index.html#mainNav">CS106A</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#courseNavbar" aria-controls="courseNavbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="courseNavbar">
<ul class="navbar-nav mr-auto">
<!-- LECTURES -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Lectures
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a
class="dropdown-item"
href="../../lectures/1-Welcome/index.html"
>
1. Welcome
</a>
<a class="dropdown-item" href="../../lectures/2-ControlFlow/index.html">
2. Control Flow
</a>
<a class="dropdown-item" href="../../lectures/3-Decomposition/index.html">
3. Decomposition
</a>
<a class="dropdown-item" href="../../lectures/4-Variables/index.html">
4. Variables
</a>
<a class="dropdown-item" href="../../lectures/5-ArithmeticExpressions/index.html">
5. Arithmetic Expressions
</a>
</div>
</li>
<!-- PSETS -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Assignments
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a
class="dropdown-item"
href="https://docs.google.com/forms/d/e/1FAIpQLSfeKsiN9eJ-zWBqKUNuTLsm3sGJ6zfsG6v9DNvZPPUnAfnRAg/viewform?usp=sf_link"
target="_blank"
>
0 - Who are you?
</a>
<a class="dropdown-item"
href="../../assn/karel.html"
>
1 - Karel
</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">
Section
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item"
href="http://cs198.stanford.edu/cs198/auth/default.aspx"
target="_blank">
Section Management
</a>
<hr />
<a class="dropdown-item"
href="../../section/section1/section1.html"
target="_blank">
Section 1
</a>
<a class="dropdown-item" href="../../section/section1/Section1.zip">
Section 1 Project
</a>
<a class="dropdown-item" href="https://repl.it/@brahmcapoor/CS106AKarelSection" target="_blank">
Section 1 Online IDE
</a>
</div>
</li>
<!-- Handouts -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Handouts
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="../../handouts/01-general-information.pdf">General Information</a>
<a class="dropdown-item" href="../../handouts/02-course-placement.pdf"> Course Placement</a>
<a class="dropdown-item" href=../../handouts/03-honor-code.pdf>Honor Code</a>
<a class="dropdown-item" href=../../handouts/installingpycharm.html>Installing PyCharm</a>
<a class="dropdown-item" href=../../assn/04-using-karel-with-pycharm.pdf>Using Karel in PyCharm</a>
<a class="dropdown-item" href=../../handouts/06-submitting-assignments.pdf>Submitting Assignments</a>
</div>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a class="nav-link" href="../../schedule.html">
<i class="fas fa-th-large"></i> Schedule </a>
</li>
</ul>
</div>
</div>
</nav> <div class="container">
<p>Today: loose ends: looking at error messages, more string functions, what is computer hardware?
<h1>Debugging</h1>
<p>Many times you run your code and see an error message. Here are some example debug error messages and how to decode them.
<p>These examples use our old <a href=movie.zip>movie.zip</a> example, editing movie-solution.py to put in some bugs and see what they do.
<h2>Bug and Symptom</h2>
<p>A "bug" is a flaw in the code - the code as written does not do what we wanted. Python doe s not notice the bug right away typically. Python does not know what we wanted, it just does what the lines say until it hits an error case, like a bad index or non-existent function name.
<p>When Python hits an error and halts - we'll think of that as the "symptom" you observe. Your job is to work backwards from the symptom to figure out the underlying bug. Very often the symptom is right on the line with the bug, and so it's pretty straightforward. But sometimes it can be more complicated.
<h2>scroll_left Grid code</h2>
<p>Here is the correct code for our scroll_left() example. Try putting in various bugs to see the error messages. Run the Doctests to see what we get.
<pre>
def scroll_left(grid):
"""
Implement scroll_left as in lecture notes.
>>> grid = Grid.build([['a', 'b', 'c'], ['d', None, None]])
>>> scroll_left(grid)
[['b', 'c', None], [None, None, None]]
"""
for y in range(grid.height):
for x in range(grid.width):
# Copying towards the left
# x, y is source, copy to x-1, y
val = grid.get(x, y)
if x > 0 and val != None:
grid.set(x - 1, y, val)
grid.set(x, y, None)
return grid
</pre>
<h2>1. Bad Function Name: <code>grid.geet()</code></h2>
<p>Put a bug in scroll_left() function: <code>grid.geet(x, y)</code>
<pre>
for y in range(grid.height):
for x in range(grid.width):
# Copying towards the left
# x, y is source, copy to x-1, y
val = <font color=red>grid.geet(x, y)</font> # line 59
</pre>
<p>Run the Doctests, the run gets an error. Look at the bottom of the error listing first, work your way upwards. The error identifies the line with the problem, it's a line in your own code. and the error message is reasonable. This is a common, easy case - Python notices the problem right on the line with the wrong code.
<pre>
....
File "<doctest scroll_left[1]>", line 1, in <module>
scroll_left(grid)
File "...movie/movie-solution.py", line 59, in scroll_left
val = grid.geet(x, y)
AttributeError: 'Grid' object has no attribute 'geet'
</pre>
<h2>2. Error In Downstream Function</h2>
<p>Suppose a() passes bad data to b(), then b() uses the data and crashes. The error message will be in b(), but the real bug is "upstream" in a().
<p>Bug: change x value to be wrong before grid.get() is called...
<pre>
for y in range(grid.height):
for x in range(grid.width):
# Copying towards the left
# x, y is source, copy to x-1, y
val = grid.get(<font color=red>x + 1000</font>, y) # line 59
</pre>
<p>Here is the error message - look at the bottom of the listing and work your way upwards, looking for your function (movie-solution.py) vs. some function you called (grid.py).
<pre>
....
File ".../movie/movie-solution.py", line 59, in scroll_left
val = grid.get(x + 1000, y)
File ".../movie/grid.py", line 76, in get
return self.array[y][x]
IndexError: list index out of range
</pre>
<p>Look at the bottom of the error listing and work up. Index-out-of-range error is not our code, it's in grid.py which we did not write. Look up one function, and we see "movie-solution.py line 59", <code>grid.get(x + 1000, y)</code>. This is line that called get() with bad data, leading it to crash.
<p>Or more likely, the logic just above line 59 was missing a < test, or had wrong arithmetic so it <b>set</b> x incorrectly, and that led to the index out of bounds error down in grid.py.
</div>
</body>