Module 3 ยท Reusable code and honest errors Lesson 9 of 15
Reading an error instead of fearing it
A traceback is a map, not a punishment: the last line names the error, and the line above it points at the file and line number where it happened.
The concept
A traceback is a map. Read it from the bottom: the last line names the error, the line above it gives the file and the line number where it happened, and everything above that is the path the program took to get there. ValueError: invalid literal for int() with base 10: "abc" means you tried to convert text that is not a number.
Four errors cover most of your first month. NameError โ you used a variable that does not exist, usually a typo. TypeError โ you mixed a string and a number. IndexError โ you asked a list for a position it does not have. IndentationError โ a block's spaces are wrong. None of them mean you are bad at this; they mean the program told you exactly which line to look at.
value = "abc"
try:
number = int(value)
except ValueError as e:
print("Could not convert:", e) # invalid literal for int()...Common mistakes
The habit worth building now: read the LAST line first, look at that line number, and fix one thing at a time. The mistake is scrolling to the top of a long traceback, feeling lost, and rewriting the whole file โ which throws away working code and usually adds the same bug back.
Worked example
ValueError: invalid literal for int() with base 10: "abc" tells you exactly which conversion failed โ you passed text where a number was expected.
Your practice task
Break your own program in three different ways on purpose and write down, in one line each, what the traceback told you.
Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.
Key takeaway
A traceback is a map. Read it from the bottom: the last line names the error, the line above it gives the file and the line number where it happened, and everything above that is the path the program took to get there.
Keep your place in this course
A free account saves which lessons you have finished and unlocks the certificate. The lesson itself stays free to read.
Create free account or sign inCourse complete
You finished every lesson.
Your certificate is ready, and the projects you built are the proof to attach to an application.
Almost there
15 lessons left of 15
Finish every lesson to unlock the certificate. Each one ends in a practice task โ do them and the final project brief writes itself.