-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec-error.ptx
250 lines (208 loc) · 7.29 KB
/
sec-error.ptx
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
<?xml version="1.0" encoding="UTF-8"?>
<!--*****************************************
This is part of Basic Programming
Copyright (C) 2024
Phạm Công Vinh
See the file COPYING for copying conditions.
******************************************-->
<section xml:id="sec-error" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Errors</title>
<objectives>
<ul>
<li>Learn about errors and ways to deal with them.</li>
</ul>
</objectives>
<introduction>
<p>
Congratulations! You have equipped yourself with quite a few programming fundamentals.
</p>
<p>
Before we continue with harder concepts, you should learn to deal with <term>errors</term>. This helps improve your learning journey and experience.
</p>
<note>
<idx><h>Overviews</h><h>of errors</h></idx>
<p>
Errors, if any, will be shown in the <term>terminal</term>. Some additional pieces of information accompanying an error are:
<ul>
<li>
<p>
The line on which the error is spotted
</p>
</li>
<li>
<p>
The type of error
</p>
</li>
<li>
<p>
A description of the error
</p>
</li>
<li>
<p>
<etc></etc>
</p>
</li>
</ul>
</p>
</note>
<p>
Below are some examples:
</p>
<aside>
<title>Try It Out</title>
<p>
Run the examples and read the errors.
</p>
<p>
Then, try to fix them.
</p>
</aside>
<sage>
<input>
print("Hello world!")
print("This is a string.")
print("This is another string.)
</input>
</sage>
<problem>
<pre>
Cell In [1], line 3
print("This is another string.)
^
SyntaxError: unterminated string literal (detected at line 3)
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>syntax error</h></idx>
<p></p>
<p>
An error is spotted in line 3, which is of type <c>SyntaxError</c>.
</p>
<p>
Reading the description and taking another look at line 3, we can easily see that we are missing a quotation mark.
</p>
</investigation>
<p>
Another example:
</p>
<sage language="python">
<input>
a = 5
s = "string"
print(a + s)
</input>
</sage>
<problem>
<pre>
TypeError
Cell In [1], line 3
...
TypeError: unsupported operand type(s) for +: 'int' and 'str'
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>type error</h></idx>
<p></p>
<p>
An error of type <c>TypeError</c> is spotted at line 3.
</p>
<p>
The description simply means that addition between an integer and a string is unsupported.
</p>
</investigation>
<p>
The final example uses an external function from the library Numpy that simply calculates the square root.
</p>
<sage language="python">
<input>
import numpy as np # Use library Numpy
print(np.sqrt("hello?"))
</input>
</sage>
<problem>
<pre>
TypeError
Cell In [1], line 2
...
TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>type error</h></idx>
<p></p>
<p>
An error of type <c>TypeError</c> is spotted at line 2.
</p>
<p>
The description might be long, but essentially it reads <q>a string cannot be passed into the function sqrt</q>.
</p>
</investigation>
<p>
But what to do if you're still confused after reading the error?
</p>
</introduction>
<subsection xml:id="subsec-fix-error">
<title>How To Fix Errors</title>
<p>
There is no programmer, however skilled, who is able to memorize every potential error. It's totally normal for a senior programmer to look up errors on the Internet, even ones they've fixed before. But of course, the more errors they deal with, the more experience they accumulate.
</p>
<p>
Coming up are some of my personal experiences in fixing errors. You can try the following approaches:
</p>
<p>
<term>1. Fix it</term><mdash />In the case the error is easy to understand, just fix it. Otherwise, continue with the next step.
</p>
<p>
<term>2. Search it on the Internet</term><mdash />Copy and paste its description onto Google.
</p>
<insight>
<idx><h>Insights</h><h>searching</h></idx>
<p>
You should include the programming language being used as a keyword to increase efficiency. For instance, <q>python unterminated string literal...</q>.
</p>
</insight>
<insight>
<idx><h>Insights</h><h>documentation</h></idx>
<p>
You can refer to documentation in your language of choice. But it is commonly the case that English resources are the best in terms of availability and being up-to-date.
</p>
</insight>
<p>
The majority of errors you can run into have already been solved by others. Thus, you can quickly read online posts and discussions and learn their solutions. Some of the most popular forums are <term>Stack Overflow</term>, <term>Github</term>, <term>Stack Exchange</term>, <term>Reddit</term>.
</p>
<p>
Here, if you're still unable to fix it, or you run into a new one, you can repeat the process, or proceed to step 3.
</p>
<p>
<term>3. Describe it yourself</term><mdash />It seems others' solutions don't apply to you when using the provided description. In that case, why don't you try to detail it using your own words? Describe what you're trying to achieve, using what data types, in which language, your assumption of why the error occurs, and so on. Not working? Let's keep going.
</p>
<p>
<term>4. Use artificial intelligence (AI)</term><mdash />If possible, you should try taking advantage of coding AIs such as <term>ChatGPT</term> or <term>Github Copilot</term> to fix errors. This is becoming an increasingly popular and effective approach.
</p>
<p>
<term>5. Make a post</term><mdash></mdash>When all other means have failed, it's time to request for help from other human programmers. You can ask people you know or make a post about your problems on online programming forums.
</p>
<insight>
<idx><h>Insights</h><h>on online etiquette</h></idx>
<p>
Make sure to study online <term>etiquette</term> to know how to behave appropriately when using such forums. Since you're looking for others' help, being respectful is a must.
</p>
</insight>
</subsection>
<conclusion>
<p>
<cd>
</cd>
</p>
<exploration>
<title>Basic Programming <mdash /> Part 11: Errors & How To Fix Them</title>
<idx><h>Videos</h><h>part 11</h></idx>
<p>
Coming soon.
</p>
<video youtubeplaylist="PLBLdRr-v59vwnKvmvLtcgmAnsb2K1Ta_M" />
</exploration>
</conclusion>
</section>