Debugging: finding what went wrong
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Every programmer writes bugs; the skill is finding them. Step through one small Python file that holds three bugs: a syntax error that stops it starting, a runtime error with a traceback, and a logic error that quietly gives the wrong average. Then hunt the last one down by reproducing it, printing values, pausing at a breakpoint and explaining the code to a rubber duck, and fix it by changing one thing.
6 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01You run a file and Python prints SyntaxError before anything else happens. What does that tell you?
Python checks the whole file's grammar before running it. A syntax error, such as a missing colon, stops it before the first line runs.
02A traceback ends with: ZeroDivisionError: division by zero. Which part should you read first?
Read a traceback from the bottom up. The last line says what went wrong (the type and the message); the File lines above it say where, ending with the line that failed.
03A program runs without any error but prints 3.0 when the right answer is 4.0. What kind of bug is this?
A logic error: the code is valid and runs to the end, but it does something different from what you meant. Python cannot spot it for you.
04In the figure, print(total, count) shows 12 4 for the list [2, 4, 6]. What does that point to?
2 + 4 + 6 is 12, so total is right. There are three numbers, but count is 4, because it started at 1 before anything was counted. 12 / 4 is 3.0.
05A debugger breakpoint on line 8 pauses the program. Has line 8 run yet?
A breakpoint stops the program just before the marked line runs. That is why the answer 3.0 is not printed yet: line 8 still has to divide total by count and return it.
06You think you see the bug. What is the best next move?
Change one thing at a time. Then, if the result changes, you know which change did it, and you can check it against the answer you worked out by hand.
المسرد
- Bug
- A mistake in a program that makes it fail or give a wrong result.
- Syntax error
- Code that breaks the grammar of the language, such as a missing colon. Python reports it before running anything.
- Exception (runtime error)
- An error that happens while the program runs, such as NameError or ZeroDivisionError. Unless it is handled, it stops the program.
- Traceback
- The report Python prints for an exception: the chain of calls that led to it, with the error type and message on the last line.
- Logic error
- The program runs without an error but does the wrong thing, such as giving 3.0 instead of 4.0.
- Breakpoint
- A marked line where a debugger pauses the program so you can inspect the variables.
- Debugger
- A tool that runs a program step by step and shows its variables. Python's built-in one is pdb; breakpoint() starts it.
- Rubber duck debugging
- Explaining your code line by line, out loud, to someone or something. Saying what each line does often reveals the bug.
الملاحظات والمصادر
- The terminal card is shortened. Python prints the full path of the file, repeats each line of code under its "File" line, and since Python 3.11 marks the exact spot with ^ and ~ characters.
- Since Python 3.10 a NameError for a near-miss name ends with a hint, here: Did you mean: 'total'?
- Since Python 3.13, breakpoint() pauses on its own line; earlier versions (3.12 and before) paused on the line after it. An editor breakpoint (IDLE, VS Code) pauses before the marked line runs.
- After the fix, average([]) would still fail with ZeroDivisionError: division by zero, because an empty list has no average. Deciding what that should do is part of the design.
- An exception that nothing handles stops the program, so no line after the failing one runs.
- Python tutorial: Errors and exceptions
- Python library: Built-in exceptions
- What's New in Python 3.10: Better error messages
- Python library: traceback
- Python library: pdb, the Python debugger
- Python library: breakpoint()
- Python library: IDLE (debugger and breakpoints)
- MIT 6.0001: Introduction to Computer Science and Programming in Python (testing and debugging)
اختبار يتاح بعد الخطوة الأخيرة.