Errors & testing · 错误与测试
Three kinds of error
- A syntax error breaks the rules of Python, so the code will not run.
- A runtime error crashes the program while it is running.
- A logic error runs fine but gives the wrong answer.
三种错误
- 语法错误(syntax error)违反了 Python 的规则,代码根本无法运行。
- 运行时错误(runtime error)在程序运行过程中让它崩溃。
- 逻辑错误(logic error)能正常运行,但给出错误的答案。
Syntax errors
- These are typos: a missing
:, an unclosed quote, a wrong indent. - Python refuses to start and shows you the line.
- Fix the spelling or punctuation, then run again.
语法错误
- 这些是笔误:漏了
:、引号没闭合、缩进不对。 - Python 拒绝启动,并指给你看是哪一行。
- 修正拼写或标点,然后重新运行。
Runtime errors
- The code starts, then hits something it cannot do.
- Examples:
10 / 0, orint("hello"), or reading past the end of a list. - The program stops at that line with a message.
运行时错误
- 代码开始运行,然后碰到了做不到的事情。
- 例如:
10 / 0,或int("hello"),或读取超出列表末尾的位置。 - 程序会在那一行停下,并显示一条消息。
Logic errors
- The program runs to the end but the result is wrong.
- Example: using
-where you meant+, or stopping a loop too early. - These are the hardest to find — the computer did exactly what you wrote.
逻辑错误
- 程序一直运行到结束,但结果是错的。
- 例如:本想用
+却用了-,或循环停得太早。 - 这些最难找 —— 计算机完全照你写的去做了。
Read the traceback
- When a program crashes, Python prints a traceback.
- Read the last line first: it names the error and what went wrong.
- The line number tells you where to look.
读懂回溯信息
- 程序崩溃时,Python 会打印一段回溯信息(traceback)。
- 先读最后一行:它说明了错误类型和出了什么问题。
- 行号会告诉你该去哪里查看。
Traceback (most recent call last):
File "<program>", line 2, in <module>
print(10 / 0)
ZeroDivisionError: division by zero
Test with chosen data
- Do not trust code until you have tested it.
- Use normal data, boundary data (like
0), and extreme data. - Check each answer against what you worked out by hand.
用选定的数据测试
- 在测试之前,不要相信代码是对的。
- 用正常数据、边界数据(例如
0)和极端数据。 - 把每个答案和你手算的结果对照检查。
def square(n):
return n * n
print(square(3)) # normal -> 9
print(square(0)) # boundary -> 0
print(square(-2)) # negative -> 4
Common mistakes
- A syntax error stops the program before it runs, so fix those first.
- Catch a specific error with
except ValueError:, not a bareexcept:. - Test the tricky cases (empty, zero, the biggest), not just the easy one.
常见错误
- 语法错误会在程序运行前就中止它,所以先修这些。
- 用
except ValueError:捕获具体错误,不要用光秃秃的except:。 - 测试棘手的情况(空、零、最大值),不要只测简单的。
Now you try
- Each task has a logic error to fix, or a crash to prevent.
- Press Check answer to test your fix against several cases.
现在轮到你
- 每个任务都有一个逻辑错误要修复,或有一个崩溃要预防。
- 按检查答案,用多组数据测试你的修复。
Find and fix a bug · 找出并修复 bug
Testing is a cycle: run it, see what's wrong, fix, run again. · 测试是一个循环:运行、看哪里错、修复、再运行。
This function should add · 添加 a and b, but it has a logic error — it subtracts. Fix it so it returns the sum. · 这个函数本应把 a 和 b 相加,但有一个逻辑错误 —— 它做的是减法。修复它,让它返回两者的和。
Click Run to see the output here. · 点击“运行”查看此处输出。
sum_to(n) should return 1 + 2 + ... + n, but it stops one number too early (an off-by-one logic error). Fix the range so it includes n. · sum_to(n) 应返回 1 + 2 + ... + n,但它提前少算了一个数(差一错误)。修正 range,让它包含 n。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write safe_divide(a, b) that returns a / b, but prevents a runtime crash: if b is 0, return 0 instead. · 编写 safe_divide(a, b),返回 a / b,但要预防运行时崩溃:如果 b 是 0,就改为返回 0。
Click Run to see the output here. · 点击“运行”查看此处输出。
average(nums) has two bugs: it divides by the wrong number, and it crashes on an empty list. Fix both, so average([2, 4, 6]) is 4 and average([]) is 0. · average(nums) 有两个错误:它除以了错误的数,而且遇到空列表会崩溃。把两个错误都改好,让 average([2, 4, 6]) 等于 4,average([]) 等于 0。
Click Run to see the output here. · 点击“运行”查看此处输出。