An error message is the computer telling you exactly what it could not do, and usually where. It is the most useful text on your screen, and most beginners skim past it.
This guide gives you a method that works in any language.
Every error tells you three things#
Traceback (most recent call last):
File "shop.py", line 12, in <module>
total = price + quantity
TypeError: unsupported operand type(s) for +: 'int' and 'str'- Where: file
shop.py, line 12 - What the code was doing:
total = price + quantity - What went wrong: it tried to add a number and a piece of text
You now know the cause without running anything: quantity is text, probably straight from input(). The fix is int(quantity).
The five-step method#
1. Read the last line first#
In Python, the bottom line names the error type and describes it. In JavaScript, the first line does. Everything else is context about how you got there.
2. Find the line number, and check the line above it too#
The reported line is where the program gave up, which is not always where the mistake is. An unclosed bracket on line 11 is reported on line 12.
3. Say the message out loud in your own words#
“Cannot read properties of undefined, reading ‘name'” becomes “something I thought was an object is actually undefined, and I asked it for .name“. Now you are looking for where that thing was supposed to be set.
4. Print what you assumed#
Most bugs are a wrong assumption about a value. Check it:
print("DEBUG quantity:", quantity, type(quantity))
total = price + quantityconsole.log("DEBUG user:", user);
console.log(user.name);This one habit will solve more bugs than any other technique.
5. Change one thing, then run it again#
Changing three things at once means you will not know which one mattered — and you may add a new bug while fixing the old one.
The three kinds of error#
Syntax errors — the code will not run at all#
A missing colon, bracket or quote. The program never starts. These are the easiest to fix because the message points near the spot.
if age > 18 # SyntaxError: expected ':'
print("ok")Runtime errors — it starts, then stops#
The syntax is fine, but something goes wrong while running: a missing file, a wrong type, dividing by zero. These come with a traceback.
Logic errors — it runs, and the answer is wrong#
The hardest kind, because nothing complains. The computer did exactly what you said; what you said was wrong.
average = a + b / 2 # runs fine, wrong answer
average = (a + b) / 2 # what you meantWhen there is no error but the output is wrong#
- Narrow it down. Print values at three points and find where the number stops being right.
- Check the boundaries. Empty list, zero, one item, negative numbers, the last item in a loop.
- Check your types. Text sorts differently from numbers, and “10” is less than “9” as text.
- Explain the code to someone. Out loud, line by line. You will usually catch it mid-sentence.
Asking for help well#
Whether you are asking a person or an AI assistant, include:
- The full error message, copied as text rather than described
- The code around the reported line, not just that one line
- What you expected to happen
- What you already tried
More on this in how to ask AI better programming questions.
Questions people ask#
Why is the line number sometimes wrong?
Because the interpreter only notices the problem later. An unclosed bracket is reported on the next line, since that is where the statement stopped making sense. Always check upward.
Should I use try/except everywhere?
No. Catching errors you do not understand hides bugs. Use it for things genuinely outside your control — a missing file, a failed network request, invalid user input.
What is a debugger?
A tool that pauses your program so you can inspect every variable at that moment. Print statements get you a long way; a debugger is faster once you are past the basics. See debugging JavaScript in the browser.
Where to go next#
- Common Python errors and how to fix them
- Common JavaScript errors and what they mean
- How to read a stack trace
- Why is my Python code not working? — a triage checklist