Understanding error messages
Let’s look at a (fictional) part of our pet shelter program:
def add_pet(pets, name, age, species):
pet = {"name": name, "age": age, "species": species, "fed": False}
pets.append(pet)
add_pet(my_pets, "Zia", 2, "cat")
Oops. We get an error:
Traceback (most recent call last):
File "shelter.py", line 5, in <module>
add_pet(my_pets, "Zia", 2, "cat")
NameError: name 'my_pets' is not defined
Let’s decode this traceback:
File "shelter.py"tells us which file has the error.line 5is the line where the error occurred.NameErroris the type of error.name 'my_pets' is not definedtells us where we have the actual problem.
Now we know: we forgot to define my_pets before using it. This means that the my_pets variable doesn’t exist. In order to be able...