Skip to content

Commit 201b90a

Browse files
authored
Add files via upload
1 parent 40b3e0b commit 201b90a

16 files changed

+5958
-0
lines changed
Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,344 @@
1+
# 30天Python编程挑战:第15天 - Python类型错误
2+
3+
- [Day 15](#day-15)
4+
- [Python错误类型](#python错误类型)
5+
- [SyntaxError (语法错误)](#syntaxerror-语法错误)
6+
- [NameError (名称错误)](#nameerror-名称错误)
7+
- [IndexError (索引错误)](#indexerror-索引错误)
8+
- [ModuleNotFoundError (模块未找到错误)](#modulenotfounderror-模块未找到错误)
9+
- [AttributeError (属性错误)](#attributeerror-属性错误)
10+
- [KeyError (键错误)](#keyerror-键错误)
11+
- [TypeError (类型错误)](#typeerror-类型错误)
12+
- [ImportError (导入错误)](#importerror-导入错误)
13+
- [ValueError (值错误)](#valueerror-值错误)
14+
- [ZeroDivisionError (零除错误)](#zerodivisionerror-零除错误)
15+
- [💻 练习 - 第15天](#-练习---第15天)
16+
17+
# 📘 Day 15
18+
19+
## Python错误类型
20+
21+
当我们编写代码时,常常会出现打字错误或其他常见错误。如果我们的代码运行失败,Python解释器会显示一条消息,提供有关问题发生位置和错误类型的反馈信息。有时它还会给我们提供可能的修复建议。了解编程语言中不同类型的错误将帮助我们快速调试代码,并使我们在编程技能上有所提高。
22+
23+
让我们一一查看最常见的错误类型。首先,让我们打开Python交互式shell。转到你的计算机终端并输入'python'。Python交互式shell将会被打开。
24+
25+
### SyntaxError (语法错误)
26+
27+
**示例1: SyntaxError**
28+
29+
```python
30+
asabeneh@Asabeneh:~$ python
31+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
32+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
33+
Type "help", "copyright", "credits" or "license" for more information.
34+
>>> print 'hello world'
35+
File "<stdin>", line 1
36+
print 'hello world'
37+
^
38+
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
39+
>>>
40+
```
41+
42+
如你所见,我们犯了一个语法错误,因为我们忘记用括号括起字符串,而Python已经提出了解决方案。让我们修复它。
43+
44+
```python
45+
asabeneh@Asabeneh:~$ python
46+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
47+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
48+
Type "help", "copyright", "credits" or "license" for more information.
49+
>>> print 'hello world'
50+
File "<stdin>", line 1
51+
print 'hello world'
52+
^
53+
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
54+
>>> print('hello world')
55+
hello world
56+
>>>
57+
```
58+
59+
错误是一个_SyntaxError_。修复后,我们的代码顺利执行。让我们看看更多的错误类型。
60+
61+
### NameError (名称错误)
62+
63+
**示例1: NameError**
64+
65+
```python
66+
asabeneh@Asabeneh:~$ python
67+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
68+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
69+
Type "help", "copyright", "credits" or "license" for more information.
70+
>>> print(age)
71+
Traceback (most recent call last):
72+
File "<stdin>", line 1, in <module>
73+
NameError: name 'age' is not defined
74+
>>>
75+
```
76+
77+
从上面的消息可以看出,名称age没有被定义。是的,确实如此,我们没有定义age变量,但我们试图像已经声明了一样打印它。现在,让我们通过声明变量并为其赋值来修复这个问题。
78+
79+
```python
80+
asabeneh@Asabeneh:~$ python
81+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
82+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
83+
Type "help", "copyright", "credits" or "license" for more information.
84+
>>> print(age)
85+
Traceback (most recent call last):
86+
File "<stdin>", line 1, in <module>
87+
NameError: name 'age' is not defined
88+
>>> age = 25
89+
>>> print(age)
90+
25
91+
>>>
92+
```
93+
94+
错误类型是_NameError_。我们通过定义变量名来调试了错误。
95+
96+
### IndexError (索引错误)
97+
98+
**示例1: IndexError**
99+
100+
```python
101+
asabeneh@Asabeneh:~$ python
102+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
103+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
104+
Type "help", "copyright", "credits" or "license" for more information.
105+
>>> numbers = [1, 2, 3, 4, 5]
106+
>>> numbers[5]
107+
Traceback (most recent call last):
108+
File "<stdin>", line 1, in <module>
109+
IndexError: list index out of range
110+
>>>
111+
```
112+
113+
在上面的例子中,Python抛出了一个_IndexError_,因为列表的索引只有0到4,所以索引5超出了范围。
114+
115+
### ModuleNotFoundError (模块未找到错误)
116+
117+
**示例1: ModuleNotFoundError**
118+
119+
```python
120+
asabeneh@Asabeneh:~$ python
121+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
122+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
123+
Type "help", "copyright", "credits" or "license" for more information.
124+
>>> import maths
125+
Traceback (most recent call last):
126+
File "<stdin>", line 1, in <module>
127+
ModuleNotFoundError: No module named 'maths'
128+
>>>
129+
```
130+
131+
在上面的例子中,我故意在math后面添加了一个多余的s,结果抛出了_ModuleNotFoundError_。让我们通过从math中删除多余的s来修复它。
132+
133+
```python
134+
asabeneh@Asabeneh:~$ python
135+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
136+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
137+
Type "help", "copyright", "credits" or "license" for more information.
138+
>>> import maths
139+
Traceback (most recent call last):
140+
File "<stdin>", line 1, in <module>
141+
ModuleNotFoundError: No module named 'maths'
142+
>>> import math
143+
>>>
144+
```
145+
146+
我们修复了它,所以让我们使用math模块中的一些函数。
147+
148+
### AttributeError (属性错误)
149+
150+
**示例1: AttributeError**
151+
152+
```python
153+
asabeneh@Asabeneh:~$ python
154+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
155+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
156+
Type "help", "copyright", "credits" or "license" for more information.
157+
>>> import maths
158+
Traceback (most recent call last):
159+
File "<stdin>", line 1, in <module>
160+
ModuleNotFoundError: No module named 'maths'
161+
>>> import math
162+
>>> math.PI
163+
Traceback (most recent call last):
164+
File "<stdin>", line 1, in <module>
165+
AttributeError: module 'math' has no attribute 'PI'
166+
>>>
167+
```
168+
169+
如你所见,我又犯了一个错误!我试图从math模块调用PI函数,而不是pi。这抛出了一个属性错误,表示该函数在模块中不存在。让我们通过将PI更改为pi来修复它。
170+
171+
```python
172+
asabeneh@Asabeneh:~$ python
173+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
174+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
175+
Type "help", "copyright", "credits" or "license" for more information.
176+
>>> import maths
177+
Traceback (most recent call last):
178+
File "<stdin>", line 1, in <module>
179+
ModuleNotFoundError: No module named 'maths'
180+
>>> import math
181+
>>> math.PI
182+
Traceback (most recent call last):
183+
File "<stdin>", line 1, in <module>
184+
AttributeError: module 'math' has no attribute 'PI'
185+
>>> math.pi
186+
3.141592653589793
187+
>>>
188+
```
189+
190+
现在,当我们从math模块调用pi时,我们得到了结果。
191+
192+
### KeyError (键错误)
193+
194+
**示例1: KeyError**
195+
196+
```python
197+
asabeneh@Asabeneh:~$ python
198+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
199+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
200+
Type "help", "copyright", "credits" or "license" for more information.
201+
>>> users = {'name':'Asab', 'age':250, 'country':'Finland'}
202+
>>> users['name']
203+
'Asab'
204+
>>> users['county']
205+
Traceback (most recent call last):
206+
File "<stdin>", line 1, in <module>
207+
KeyError: 'county'
208+
>>>
209+
```
210+
211+
如你所见,在用于获取字典值的键中有一个拼写错误。这是一个键错误,修复方法很简单。让我们来做这个!
212+
213+
```python
214+
asabeneh@Asabeneh:~$ python
215+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
216+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
217+
Type "help", "copyright", "credits" or "license" for more information.
218+
>>> user = {'name':'Asab', 'age':250, 'country':'Finland'}
219+
>>> user['name']
220+
'Asab'
221+
>>> user['county']
222+
Traceback (most recent call last):
223+
File "<stdin>", line 1, in <module>
224+
KeyError: 'county'
225+
>>> user['country']
226+
'Finland'
227+
>>>
228+
```
229+
230+
我们调试了错误,我们的代码运行并得到了结果。
231+
232+
### TypeError (类型错误)
233+
234+
**示例1: TypeError**
235+
236+
```python
237+
asabeneh@Asabeneh:~$ python
238+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
239+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
240+
Type "help", "copyright", "credits" or "license" for more information.
241+
>>> 4 + '3'
242+
Traceback (most recent call last):
243+
File "<stdin>", line 1, in <module>
244+
TypeError: unsupported operand type(s) for +: 'int' and 'str'
245+
>>>
246+
```
247+
248+
在上面的例子中,出现了TypeError,因为我们不能将数字与字符串相加。第一个解决方案是将字符串转换为intfloat。另一个解决方案是将数字转换为字符串(那么结果将是'43')。让我们采用第一种修复方式。
249+
250+
```python
251+
asabeneh@Asabeneh:~$ python
252+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
253+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
254+
Type "help", "copyright", "credits" or "license" for more information.
255+
>>> 4 + '3'
256+
Traceback (most recent call last):
257+
File "<stdin>", line 1, in <module>
258+
TypeError: unsupported operand type(s) for +: 'int' and 'str'
259+
>>> 4 + int('3')
260+
7
261+
>>> 4 + float('3')
262+
7.0
263+
>>>
264+
```
265+
266+
错误已消除,我们得到了预期的结果。
267+
268+
### ImportError (导入错误)
269+
270+
**示例1: ImportError**
271+
272+
```python
273+
asabeneh@Asabeneh:~$ python
274+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
275+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
276+
Type "help", "copyright", "credits" or "license" for more information.
277+
>>> from math import power
278+
Traceback (most recent call last):
279+
File "<stdin>", line 1, in <module>
280+
ImportError: cannot import name 'power' from 'math'
281+
>>>
282+
```
283+
284+
math模块中没有名为power的函数,它的名字是不同的:_pow_。让我们纠正它:
285+
286+
```python
287+
asabeneh@Asabeneh:~$ python
288+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
289+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
290+
Type "help", "copyright", "credits" or "license" for more information.
291+
>>> from math import power
292+
Traceback (most recent call last):
293+
File "<stdin>", line 1, in <module>
294+
ImportError: cannot import name 'power' from 'math'
295+
>>> from math import pow
296+
>>> pow(2,3)
297+
8.0
298+
>>>
299+
```
300+
301+
### ValueError (值错误)
302+
303+
```python
304+
asabeneh@Asabeneh:~$ python
305+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
306+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
307+
Type "help", "copyright", "credits" or "license" for more information.
308+
>>> int('12a')
309+
Traceback (most recent call last):
310+
File "<stdin>", line 1, in <module>
311+
ValueError: invalid literal for int() with base 10: '12a'
312+
>>>
313+
```
314+
315+
在这种情况下,我们无法将给定的字符串转换为数字,因为其中有字母'a'
316+
317+
### ZeroDivisionError (零除错误)
318+
319+
```python
320+
asabeneh@Asabeneh:~$ python
321+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
322+
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
323+
Type "help", "copyright", "credits" or "license" for more information.
324+
>>> 1/0
325+
Traceback (most recent call last):
326+
File "<stdin>", line 1, in <module>
327+
ZeroDivisionError: division by zero
328+
>>>
329+
```
330+
331+
我们不能用零去除一个数字。
332+
333+
我们已经介绍了一些Python错误类型,如果你想了解更多,请查看Python文档中关于Python错误类型的内容。
334+
如果你擅长阅读错误类型,那么你将能够快速修复你的bug,你也将成为一个更好的程序员。
335+
336+
🌕 你正在进步。你已经完成了一半的道路,正走向伟大。现在做一些练习来锻炼你的大脑和肌肉。
337+
338+
## 💻 练习 - 第15天
339+
340+
1. 打开你的Python交互式shell,尝试本节中介绍的所有示例。
341+
342+
🎉 恭喜!🎉
343+
344+
[<<14天](../14_Day_Higher_order_functions/14_higher_order_functions_cn.md) | [第16>>](../16_Day_Python_date_time/16_python_datetime_cn.md)

0 commit comments

Comments
 (0)