From c8ceb01b31fffed66dc2b14183495cd90796bb2d Mon Sep 17 00:00:00 2001 From: Archie Date: Fri, 30 Aug 2024 11:18:30 +0100 Subject: [PATCH] add 15_Day_Python_type_errors Chinese support --- Chinese/15_python_type_errors.md | 365 +++++++++++++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 Chinese/15_python_type_errors.md diff --git a/Chinese/15_python_type_errors.md b/Chinese/15_python_type_errors.md new file mode 100644 index 00000000..f5291fbd --- /dev/null +++ b/Chinese/15_python_type_errors.md @@ -0,0 +1,365 @@ +
+

30天Python:第15天——Python类型错误

+ + + + + Twitter Follow + + +作者: +Asabeneh Yetayeh +
+ 第二版: 2021 年 7 月 +
+ +
+ + +[<< 第 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 "", 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 "", 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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +ModuleNotFoundError: No module named 'maths' +>>> import math +>>> math.PI +Traceback (most recent call last): + File "", line 1, in +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 "", line 1, in +ModuleNotFoundError: No module named 'maths' +>>> import math +>>> math.PI +Traceback (most recent call last): + File "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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 "", line 1, in +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)