Errors & testing
Python Basics Lesson 14 3:01 English narration · English + 中文 subtitles burned in
Chapters
Transcript
Errors come in three kinds, and telling them apart tells you where to look.
错误分为三类,分清它们,你就知道该去哪里找。
A syntax error breaks Python's own rules — a missing bracket, a missing colon — so the program will not even start.
语法错误破坏了 Python 自己的规则——少一个括号、少一个冒号—— 所以程序根本启动不了。
A runtime error starts fine and then stops partway, when it hits something it cannot do, like dividing by zero.
运行时错误可以正常开始,但会在中途停下,因为它遇到了做不到的事,比如除以 0。
And a logic error runs all the way to the end and gives you a wrong answer.
逻辑错误则会一路跑到结束,然后给你一个错误的答案。
Only the third one keeps quiet, which is exactly what makes it dangerous.
只有第三种是不出声的,而这正是它危险的地方。
When a program crashes Python prints a traceback, and it looks like a wall of text until you know the trick: start at the bottom.
程序崩溃时,Python 会打印一段回溯信息,在你知道诀窍之前它就像一堵文字墙: 从最后一行开始看。
The last line names the error and says what went wrong, in this case division by zero.
最后一行说出错误的名字和出了什么问题,这里是“除以零”。
Then look up one or two lines for the line number — that is where to go.
然后往上看一两行找行号——那就是你要去的地方。
The middle is the route Python took to get there, which matters later, when your functions call each other.
中间那部分是 Python 走到这里的路径,等你的函数开始互相调用时,它才变得重要。
Here is the quiet one.
来看那个不出声的。
This function is meant to add, and it subtracts.
这个函数本该做加法,它却做了减法。
There is no crash, no message, no red text — just minus one where you expected five.
没有崩溃,没有提示,没有红色的字——只是你以为会得到 5,结果是 -1。
Python did exactly what you wrote, which is the whole problem: the mistake is in your thinking, not in your typing.
Python 完全照你写的做了,而这正是问题所在:错在你的想法里,不在你的打字上。
And the fix is one character.
改正只需要一个字符。
This is why the next habit matters more than any of the others.
也正因为如此,接下来这个习惯比其他任何习惯都重要。
Testing is how you find a logic error, and picking the data is the skill.
测试是你找出逻辑错误的办法,而挑选数据正是这门功夫。
Try an ordinary value first: three squared is nine.
先试一个普通的值:3 的平方是 9。
Then the boundary — zero, an empty list, the first and last item — because that is where the off-by-one mistakes live.
然后是边界——0、空列表、第一项和最后一项——因为差一错误就住在那里。
Then something awkward: a negative number, a very large one.
再来一个刁钻的:负数,或者非常大的数。
And the important part: work out the expected answer before you run it.
最关键的一点:在运行之前先把预期答案算出来。
Otherwise you are not testing, you are just admiring the output.
否则你不是在测试,只是在欣赏输出。
Now the lesson's third task.
现在做课程里的第三道题。
This function divides, and dividing by zero crashes it — a runtime error, from data you cannot control if a user is typing it.
这个函数做除法,而除以 0 会让它崩溃—— 这是一个运行时错误,而且如果数据是用户输入的,你根本控制不了。
So check before you divide: if the second number is zero, return zero instead.
所以在做除法之前先检查:如果第二个数是 0,就直接返回 0。
The crash is gone, the function still answers, and every other input behaves exactly as before.
崩溃没有了,函数照样给出答案,其他所有输入的行为和原来完全一样。
Guarding the one bad case is usually this small.
给唯一一个坏情况加个防护,通常就是这么小的一件事。
Four things to take with you.
带走四点。
One: a syntax error stops it starting; a runtime error stops it midway.
第一:语法错误让程序无法启动;运行时错误让它中途停下。
Two: a logic error runs happily and answers wrongly.
第二:逻辑错误跑得好好的,却给出错误答案。
Three: read a traceback's last line first, then its line number.
第三:读回溯信息先看最后一行,再看行号。
Four: test normal, boundary and awkward data, with the answers worked out in advance.
第四:用普通值、边界值和刁钻的数据测试,并且预先算好答案。
Now do the three tasks — two of them hide a logic error for you to find.
现在去做那三道题——其中两题藏着一个逻辑错误,等你找出来。