Learn to decode an error message instead of closing it: type, file, line and stack trace.
Open this lesson in KodokonAn error is not a punishment. Your computer is not judging you and it is not laughing at you: it has noticed that it does not know what to do, and it is telling you why. It works exactly like a GPS announcing "recalculating route" - that is not a reproach, it is information. The beginner's reflex is to close the red window without reading it. The developer's reflex is to read it slowly, all the way to the end. It is often the only moment when the machine speaks to you in plain words.
Almost every error message contains the same pieces of information. The type of error, which sorts it into a broad family. The message, which explains in one sentence what is wrong. The file involved and the line number, which tell you exactly where to look. And sometimes a stack trace: the list of steps the program went through before breaking down.
Traceback (most recent call last):
File "shopping.py", line 12, in <module>
total = price * quantity
NameError: name 'quantity' is not definedIn the example above, everything is spelled out. The file is shopping.py, the line is 12, the type is NameError and the message says name 'quantity' is not defined. In plain English: on line 12 you are using a variable quantity that you never created, or whose name you misspelled. No complicated investigation needed - the message already contained the answer.
A Python stack trace reads from the bottom up: the last line gives the type and the explanation, and just above it sit the file and the line at fault. When the trace is very long, look for the first line that mentions your own file. Lines pointing at unfamiliar files with strange names belong to the libraries you are using: 99% of the time they are not the ones with a bug, the way you are calling them is.
Uncaught TypeError: Cannot read properties of
undefined (reading 'name')
at showProfile (app.js:24:18)
at start (app.js:41:3)A handful of phrasings come up again and again, and it pays to recognize them. not defined means the name you used does not exist at that point. undefined means the thing exists but holds no value. unexpected means the program was expecting something else, typically a forgotten bracket or quote. permission denied points to a rights problem, not a code problem. And no such file or directory means the path you wrote leads nowhere.
NameError: name 'count' is not defined. Which name do you write in the blank to fix the error?price = 10
___ = 3
total = price * count
print(total)