Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 15_Day_Python_type_errors Chinese support #603

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
365 changes: 365 additions & 0 deletions Chinese/15_python_type_errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,365 @@
<div align="center">
<h1> 30天Python:第15天——Python类型错误 </h1>
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
</a>
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
</a>

<sub>作者:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a>
<br>
<small> 第二版: 2021 年 7 月</small>
</sub>

</div>
</div>

[<< 第 14 天](../14_Day_Higher_order_functions/14_higher_order_functions.md) | [第 16 天 >>](../16_Day_Python_date_time/16_python_datetime.md)

![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)

- [📘 第 15 天](#-第15天)
- [Python 错误类型](#python错误类型)
- [SyntaxError](#syntaxerror)
- [NameError](#nameerror)
- [IndexError](#indexerror)
- [ModuleNotFoundError](#modulenotfounderror)
- [AttributeError](#attributeerror)
- [KeyError](#keyerror)
- [TypeError](#typeerror)
- [ImportError](#importerror)
- [ValueError](#valueerror)
- [ZeroDivisionError](#zerodivisionerror)
- [💻 练习: 第 15 天](#练习-第15天)

# 📘 第 15 天

## Python 错误类型

当我们编写代码时,很常见会打错字或出现其他一些常见错误。如果我们的代码不能运行,Python 解释器会显示一条信息,包含关于问题发生在哪里以及错误类型的反馈信息。它有时还会给我们提供可能的修复建议。理解编程语言中的不同类型的错误将有助于我们快速调试代码,同时也会使我们在工作中变得更优秀。

让我们逐个来看最常见的错误类型。首先让我们打开 Python 交互式 Shell。打开电脑终端,输入‘python’,将会打开 Python 交互式 Shell。

### SyntaxError

**示例 1: SyntaxError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
File "<stdin>", line 1
print 'hello world'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
>>>
```

如你所见,我们犯了一个语法错误,因为我们忘了用括号包裹字符串,Python 已经建议了解决办法。让我们修复它。

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
File "<stdin>", line 1
print 'hello world'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
>>> print('hello world')
hello world
>>>
```

错误类型是*SyntaxError*。修复之后我们的代码顺利执行了。让我们看看更多的错误类型。

### NameError

**示例 1: NameError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'age' is not defined
>>>
```

如你所见,消息显示年龄变量未定义。确实,我们没有定义年龄变量,但我们试图打印它。现在,让我们通过定义并赋值它来修复它。

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'age' is not defined
>>> age = 25
>>> print(age)
25
>>>
```

错误类型是*NameError*。我们通过定义变量名调试了错误。

### IndexError

**示例 1: IndexError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> numbers = [1, 2, 3, 4, 5]
>>> numbers[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>>
```

在上面的例子中,Python 引发了 IndexError,因为列表只有 0 到 4 的索引,因此超出了范围。

### ModuleNotFoundError

**示例 1: ModuleNotFoundError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import maths
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'
>>>
```

在上面的例子中,我故意在 math 后面多加了一个 s,所以引发了*ModuleNotFoundError*。让我们通过去掉多余的 s 来修复它。

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import maths
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'
>>> import math
>>>
```

我们修复了它,让我们使用 math 模块中的一些函数。

### AttributeError

**示例 1: AttributeError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import maths
Traceback (最
澘會末呼つはゃす尽ルル귀행세뿌府ぐ
<|vq_10375|> File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'
>>> import math
>>> math.PI
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'math' has no attribute 'PI'
>>>
```

如你所见,我又犯了一个错误!我试图从 math 模块调用 PI 函数,但实际上它是 pi。它引发了属性错误,意味着该函数在模块中不存在。让我们通过将 PI 改为 pi 来修复它。

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import maths
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'
>>> import math
>>> math.PI
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'math' has no attribute 'PI'
>>> math.pi
3.141592653589793
>>>
```

现在,当我们从 math 模块调用 pi 时得到了结果。

### KeyError

**示例 1: KeyError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> users = {'name':'Asab', 'age':250, 'country':'Finland'}
>>> users['name']
'Asab'
>>> users['county']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'county'
>>>
```

如你所见,用于获取字典值的键存在拼写错误。这是一个 key error,修复非常简单。让我们做一下!

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> user = {'name':'Asab', 'age':250, 'country':'Finland'}
>>> user['name']
'Asab'
>>> user['county']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'county'
>>> user['country']
'Finland'
>>>
```

我们调试了错误,代码运行并获取了值。

### TypeError

**示例 1: TypeError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 4 + '3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
```

在上面的例子中,引发了 TypeError,因为我们不能将数字和字符串相加。第一个解决方法是将字符串转换为 int 或 float。另一种解决方法是将数字转换为字符串(结果将是'43')。让我们跟随第一种解决方案。

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 4 + '3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> 4 + int('3')
7
>>> 4 + float('3')
7.0
>>>
```

错误消除,我们得到了期望的结果。

### ImportError

**示例 1: TypeError**

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import power
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'power' from 'math'
>>>
```

数学模块中没有函数叫做 power,它是以另一个名字存在: _pow_。让我们纠正它:

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import power
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'power' from 'math'
>>> from math import pow
>>> pow(2,3)
8.0
>>>
```

### ValueError

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> int('12a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12a'
>>>
```

在这种情况下,我们无法将给定字符串更改为数字,因为它包含了'a'字母。

### ZeroDivisionError

```py
asabeneh@Asabeneh:~$ python
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
```

我们不能将一个数除以零。

我们已经涵盖了一些 Python 错误类型,如果你想了解更多,请查看 Python 文档中的 Python 错误类型。
如果你能够很好地阅读错误类型,你将能够快速修复你的 bug,并且你也会成为一个更优秀的程序员。

🌕 你正在不断进步。你已经走到了通往卓越的半程。现在来一些锻炼大脑和肌肉的练习吧。

## 💻 练习: 第 15 天

1. 打开你 Python 的交互式 Shell,尝试本节中涵盖的所有示例。

🎉 恭喜! 🎉

[<< 第 14 天](../14_Day_Higher_order_functions/14_higher_order_functions.md) | [第 16 天 >>](../16_Day_Python_date_time/16_python_datetime.md)