Chapter 3 · Course 4 c03-m04
How do you understand an error?
Your goal Tell three kinds of problems apart and follow a calm method to find and verify the first cause.
The lesson, the vocabulary, the worked example and the mission are separate blocks. You can follow them in order or return to the one you need.
Video explanation
This English explanation has English subtitles. Its examples may use a separate practice dataset; use the data stated in the written mission for your answers.
Story · optional context
The story so far
You are the new data team member at Aster Motors. Mara Sol is teaching you to prepare messages about Noctis 01 before its first test. Noor's small program does not show the expected result. A manager is in a hurry, but Mara says: “An error is not a shame. Read what the machine did, then check what we meant to ask.”
This page stands alone. print('Hello') asks Python to show Hello. You can inspect every example and do the mission on paper without Run.
Before the lesson
Purpose of this course
Tell three kinds of problems apart and follow a calm method to find and verify the first cause.
Topics covered
Recognize a writing error before running, an error during running, and a wrong result without an error message.; Use the indicated line number as a clue without assuming the cause is always exactly there.; Compare code, expected result, observed result, and error message.; Change one thing at a time and check again.
What you will learn
- Recognize a writing error before running, an error during running, and a wrong result without an error message.
- Use the indicated line number as a clue without assuming the cause is always exactly there.
- Compare code, expected result, observed result, and error message.
- Change one thing at a time and check again.
PART 1 · THE LESSON
Understand step by step
Lesson · explanation
An error is a clue, not a judgment about you
When a small program does not give the answer you expected, you may feel stuck. But the gap between expected and observed results contains useful information. Mara asks you to read it calmly: what did you want, what exact code ran, and what did the computer show? These three answers give you a place to start.
Think of building a tower with toy blocks. You wanted three straight floors, but it leans. You could knock everything down at random, or look for the first misplaced block. Debugging is like the second choice: observe, suggest one small cause, change one thing, and check whether the result changes as predicted.
To debug means to find and correct the cause of a problem. It does not merely mean making a red message disappear. A program can display a sentence without any technical error while that sentence is false. Check both how the program behaves and what its result means.
In the mission, three notes all seem to say that something is wrong. They describe different causes. Python cannot read A properly. It can read B, but the requested calculation fails. C runs and displays a claim that the workshop record does not support. Classifying them helps you choose an appropriate repair.
Pause and think
If a program shows a sentence but the original record says something else, does the absence of a red error message validate the report?
See the explanation
No. Compare the displayed sentence with the original fact. A technically successful display can still be factually wrong.
Lesson · explanation
A syntax error stops Python reading an instruction
Syntax is the way an instruction must be written for a language to understand it. In print('Noctis 01'), one parenthesis opens after print and another closes after the words. Quotes surround the text. If Noor writes print('Noctis 01', the final parenthesis is missing, so Python cannot read the whole instruction.
Imagine a sentence that opens a parenthesis and never closes it. A reader waits for the ending. Python is stricter still: some signs must be in the right places. A message such as SyntaxError often says the form of the code is wrong. It does not say whether the idea about Noctis is true; first it says the instruction is incomplete.
In the worked example, the smallest repair is to add ) at the end. The code becomes print('Noctis 01'). There is no need to change the car's name, erase the whole file, or invent another rule. One missing sign explains the gap between the expected words and the error message.
Mission note A contains print('Ready'. Find the parenthesis opened after print and ask where it closes. It does not. Add it, predict the output Ready without code punctuation, and run the fixed line if the workspace is available.
Pause and think
What single edit turns print('Ready' into a readable instruction?
See the explanation
Add the closing parenthesis to make print('Ready'), then predict and check the output Ready.
Lesson · explanation
A runtime error happens after readable code starts
A runtime error happens when the program begins following readable instructions but one requested action fails. print(1 / 0) has its parentheses. Python understands that it should calculate 1 / 0 and then display the result. But division by zero has no ordinary numerical answer to show, so Python stops and reports a problem.
Imagine a recipe clearly saying divide one cake into zero equal portions. The sentence is readable, but the action cannot be done. Adding a comma to the recipe would not help; you must change the requested operation. For mission note B, adding random parentheses cannot fix the division by zero.
A message such as ZeroDivisionError names the technical cause. You do not need to memorize it today. Ask what the program tried to do and why that cannot succeed. Then return to Mara's real question: what calculation did she want? She needs a valid divisor or a different rule, not a made-up answer.
The difference from syntax matters. In A, Python cannot read the instruction correctly. In B, it can read it, starts work, and fails on the operation. Both may show a red message, but each repair must address its own cause.
Pause and think
Why can you not fix print(1 / 0) simply by adding a parenthesis?
See the explanation
The parentheses are already present. The operation 1 / 0 cannot produce a number, so the calculation rule must change.
Lesson · explanation
A false result can appear without an error message
The third problem is quieter. print('Check finished') is an instruction Python can follow. The screen shows Check finished, and no technical error appears. But if the workshop record says check planned, the message changes the meaning of the situation. The program works while the report would mislead people.
Think of a printer making a poster saying match won before the game has started. The printer printed the requested words correctly; it did not watch the game. Likewise, Python does not inspect workshop records when it displays a sentence. A person must read the source and choose words supported by it.
In mission note C, do not hunt for a missing parenthesis. The line is technically readable. Change finished to planned so that the displayed words match the record. Predict the new output, run if possible, and compare it with the original document.
Even after the change, do not say that the check will certainly be completed on time. Planned describes an intention. To say finished, the team would need a new observation or a completed check record. A report must say exactly what its evidence supports.
Pause and think
Note C produces no error message. What evidence still shows its wording is wrong?
See the explanation
The workshop record says check planned, while the program says check finished. Those statements do not mean the same thing.
Lesson · explanation
A line number tells you where to start looking
An error message may show a line number. If Python mentions line two, begin by reading line two. Also inspect the line before it: an open parenthesis on line one can make line two difficult to read. The number guides your search, but it does not always prove where the first cause began.
Imagine finding a puddle under a doorway. You see water in the hall, but the leak may begin in the room next door. A line number often points to where a problem becomes visible. To find its cause, follow code signs backward: parentheses, quotes, and instruction order. Do not edit a line solely because its number appears.
In the example, line two is print('Noctis 01'. It has no closing parenthesis, so the message pointing nearby is useful. Read one sign at a time: print, (, the quoted text, and the end. You can see that ) is missing. That observation justifies the fix better than a random guess.
Some problems show no line number at all. A report may tell a false story even though the program runs without a technical error. For C, the line to investigate comes from comparing the text with the workshop record, not from Python's message. Choose your search method according to the type of problem.
Pause and think
If an error names line two, should you ignore line one completely?
See the explanation
No. Line two is a good starting point, but an unclosed parenthesis or quote on the previous line may be the cause.
Lesson · explanation
Change one thing, then check two questions
A calm method has five actions. First, write the expected output. Second, keep the exact code that ran. Third, note the output or error actually observed. Fourth, suggest one possible cause and make the smallest relevant change. Fifth, predict what the change should do and try again, or reason through it on paper.
For A, add the missing parenthesis and check that Ready appears. For B, recognize that 1 / 0 is impossible and ask which calculation was intended. For C, change finished to planned and compare the words with the record. Three problems need three different investigations; classifying them prevents a repair aimed at the wrong thing.
After every repair, ask two separate questions. Did the program behave as expected? Does its result describe the real situation accurately? A print instruction can pass the first question and fail the second. Both checks protect the team from a public message that is correct for the computer but misleading for people.
If your first idea does not work, keep the observation and try another small hypothesis. Changing everything at once hides which step helped. During the Noctis 01 investigation, this discipline will matter when a number looks suspicious and people are rushing.
Pause and think
After adding ) to A, the message appears. What else must you check if it describes the car's status?
See the explanation
Read the relevant records or ask responsible people whether the claim is true. Correct display is not enough.
PART 2 · WATCH IT WORK
A complete worked example
Follow each step, then the result
Worked example: one missing bracket
Noor wants Noctis 01 but line 2 is print('Noctis 01'. The workspace points to a syntax error near line 2.
- Expected:
Noctis 01on screen. Observed: an error message instead. - Read line 2.
print(opens a bracket; the message ends with a quote but no closing bracket. - Make one change:
print('Noctis 01'). - Predict output
Noctis 01, run if available, then compare observation with prediction. - Even if the name appears, do not claim the car was checked. The line displays only a name.
The line number tells us where to begin; character-by-character comparison explains the cause.
How this helps with your missionThe missing-parenthesis example teaches you to compare expected output, observed behavior, and the exact code before making a small repair. In the mission, that method fixes A with ), but it also reveals that B fails for a different reason: division by zero. For C, it leads you beyond technical messages to compare finished with the workshop record's planned before the meeting.
PART 3 · YOUR TURN
Use the lesson to solve the mission
Story scene · practical task
Mission: sort three incidents before the meeting
Mara gets three notes about a Noctis 01 message. A: print('Ready' causes a syntax message on line 1. B: print(1 / 0) stops during running. C: print('Check finished') displays “Check finished,” but the workshop card says “check planned.” The story's meeting is approaching; your learning has no time limit.
Your task
- Classify A, B, and C as writing, running, or wrong-result problems.
- Fix A by changing as little as possible.
- Explain why B fails although its brackets are present.
- For C, suggest a faithful message and say how to check it.
How to know you have succeeded
- You match A to syntax, B to runtime, and C to meaning.
- You add just A's closing bracket.
- You recognize division by zero has no result.
- You replace “finished” with “planned” and compare output with the original card.
Try code or SQL and keep a local draft. The mission questions are checked separately below.
See the worked answer
- A is a writing error: the instruction is not closed. B happens during running: Python reads it but cannot calculate
1 / 0. C is wrong for the real world: the program works while distorting the card. - Smallest fix for A:
print('Ready'). - For B, the brackets are correct; the requested calculation is impossible. Rethink the rule instead of randomly adding punctuation.
- For C,
print('Check planned')matches the card. Predict that output, inspect the display, and keep the card as evidence of the real status.
You can now read an error without panic. Next, you will meet Python values and begin simple calculations.
Before you leave
Remember this
- A syntax error means Python cannot read the instruction as written.
- A runtime error occurs when a readable instruction requests an action that fails.
- A program can run without an error and still display a false claim about the world.
- A line number guides your search; the cause may begin just before it.
- Compare expected behavior, observations, and the source; change one thing at a time and recheck both behavior and meaning.
Part of the lesson · keep nearby
New words, explained
These are the names of ideas used in the lesson above. Return here whenever a word feels unfamiliar.
- Syntax error
- Python cannot read an instruction's writing, for example because a bracket is missing.
- Runtime error
- The instruction can be read, but an action fails while the program runs.
- Wrong result
- The program runs without a technical error but answers the wrong question.
- Line number
- A line's position in the program. It helps us choose where to inspect first.
- Debug
- Find and correct a problem's cause by checking each idea.