Python errors look intimidating because they're long. They're actually friendly: the last line tells you exactly what went wrong, and the line above that tells you where. Read bottom-up and ignore the rest until you're more advanced.
Traceback (most recent call last):
File "report.py", line 12, in <module>
total = price * quantitiy
NameError: name 'quantitiy' is not defined
Read bottom-up:
quantity.report.py.NameError: name 'x' is not definedMeaning: Python doesn't know what x is.
Usual cause: Typo, or you forgot to create the variable, or you tried to use it before the line that defines it.
Fix: check spelling and make sure the variable is created on an earlier line.
SyntaxError: invalid syntaxMeaning: The code doesn't follow Python's grammar.
Usual cause: Missing colon at the end of an if/for/def line. Or a bracket that wasn't closed. Or a quote that wasn't closed.
# Wrong: no colon
if age > 18
print("adult")
# Right
if age > 18:
print("adult")
IndentationErrorMeaning: Wrong amount of leading whitespace.
Usual cause: You used a tab where Python expected spaces, or you indented inconsistently.
Fix: pick four-space indentation everywhere. VS Code does this for you if you stay on the Python file mode.
TypeErrorMeaning: You're using a value of the wrong type.
Usual cause: Trying to add a number and a string.
# Wrong: "5" is a string, not a number
total = "5" + 10
# Right
total = int("5") + 10 # 15
FileNotFoundErrorMeaning: The file you asked Python to open doesn't exist at that path.
Usual cause: Typo in the filename, wrong folder, or the file extension is wrong.
Fix: print the current folder first and double-check:
import os
print(os.getcwd()) # which folder am I in?
print(os.listdir()) # what's in this folder?
NameError, SyntaxError, IndentationError, TypeError, FileNotFoundError.In a Jupyter cell, run each of these and see what error you get. Then fix it.
# 1
print(secret_number)
# 2
if 5 > 3
print("yes")
# 3
"hello" + 5
# 4
open("not_a_real_file.txt")
Reading errors gets easier the more of them you see. Welcome to programming.