Lesson 5 of 30
In progress
Read Python errors without panicking
Learn what a traceback is telling you, meet the five errors beginners hit most, and debug with print and careful reading.
An error message is not a verdict on your intelligence. It is a note from the interpreter telling you exactly where it stopped and what it expected. Most beginners lose an hour to an error they could have read in thirty seconds.
What you will learn
- Read a traceback from the bottom up.
- Recognise
SyntaxError,IndentationError,NameError,TypeErrorandValueError. - Use
printand comments to narrow down where a value goes wrong.
A traceback is a stack of clues
When Python hits a problem, it prints a traceback. The last line names the error and describes it. The lines above show the chain of calls that led there, with file names and line numbers.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
print(average([10, 20, 30]))
print(average([]))
Press Run to see the output.
The second call fails with ZeroDivisionError: division by zero. The traceback points at the return line inside average, because that is where the division happened, not at the call. Read the last line first, then walk upwards to see which call brought you there.
The five errors you will meet this week
SyntaxError means Python could not parse the line at all. A missing colon, an unclosed bracket, or a quote that never closes will do it. The reported line is often the line after the real mistake, because the parser only notices when the statement never finishes.
IndentationError means the spacing at the start of a line does not match the block structure. Python uses indentation to define blocks. Use four spaces per level and never mix tabs and spaces.
NameError means a name was used before it was assigned, or it is spelled differently from the assignment. print(scroe) is a NameError even though score exists.
TypeError means the type does not support the operation. "3" + 4 fails because one side is text and the other is a number. Convert one side deliberately.
ValueError means the type is right but the value is impossible. int("hello") is a ValueError. So is unpacking the wrong number of values.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
Run that and nothing breaks. Now change score to the string "10" and watch score / 2 fail with a TypeError. Changing one value and re-running is a legitimate debugging technique.
Debug by narrowing, not by guessing
Comments are useful for more than notes. Comment out a suspicious line, run again, and see whether the error moves. If the program now fails later, the commented line was working.
print is the beginner’s debugger. Print the value just before the line that fails, with a label:
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
The DEBUG prefix makes it obvious which lines to remove afterwards. Printing the type as well as the value catches conversions you forgot.
You can deliberately raise an error to stop a program when a value is impossible:
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
print(set_age(14))
Press Run to see the output.
Run the smallest version of the failing code. If a 40-line program fails, copy the five lines around the error into a new file. Half of all debugging is removing code until the problem is obvious.
The most common mistake is changing three things at once and then not knowing which change fixed it. Change one line, run again, and keep a note of what you changed. If the change made things worse, you can undo exactly one thing.
This program should print a receipt, but it fails. Fix it and explain in one sentence what was wrong.
price = 250
quantity = "3"
total = price * quantity
print(f"Total: {total}")
Show solution
quantity is text, so multiplying a number by text raises a TypeError. Convert the input value to an integer.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
If the quantity really comes from input(), the fix is quantity = int(input("Quantity: ")).
Recap
- Read the last line of a traceback first, then the line numbers above it.
- The five common errors each point at a different kind of mistake: syntax, spacing, names, types and values.
- Change one thing at a time, print labelled values, and run the smallest failing example.
You now have the fundamentals: a working Python, variables, text, numbers and readable errors. Next, you will make programs decide things with if, elif and else.
Why does the traceback point at a line that looks correct?
Should I delete all my debug prints before sharing code?
Is it normal to get errors every day?
Progress is saved in this browser only. No account, no email.