CurriculumReading Python Error Messages, for KidsName that exception
Common Python Errors: IndexError, KeyError, TypeError Explained
Recognise the common crash types by name, the way a symptom points to a diagnosis.
Name that exception
You've met NameError. There are a few more crash TYPES worth recognising on sight — each one is a different diagnosis.
IndexError means: you asked a LIST for a position that doesn't exist.
hoard = ["gold", "gems"]
print(hoard[5])Output
IndexError: list index out of rangeKeyError means: you asked a DICT for a key that was never set.
dragon = {"name": "Ember"}
print(dragon["mana"])Output
KeyError: 'mana'TypeError means: you tried to combine two things that don't mix — most often, text and a number joined with +.
print("Level: " + 5)Output
TypeError: can only concatenate str (not "int") to strValueError means: you tried to CONVERT text that was never a real number in the first place.
int('abc')Output
ValueError: invalid literal for int() with base 10: 'abc'ZeroDivisionError means exactly what it says: dividing by zero, which has no real answer.
print(10 / 0)Output
ZeroDivisionError: division by zeroFive names, five different symptoms. Once you recognise the TYPE, you already know roughly where to look — before reading a single word of the message.
Check your understanding
party = ["Ember", "Blaze"]. print(party[9]) raises which error?
party = ["Ember", "Blaze"]
print(party[9])Why: IndexError — party only has positions 0 and 1. Asking for position 9 is asking for something genuinely out of range, and IndexError is the specific name for exactly that.
score = int("high"). What error does this raise, and why?
score = int("high")Why: ValueError. int() happily accepts a string — that's normal — but "high" isn't digits, so there's nothing valid to convert into a number. ValueError specifically means "this value's CONTENT doesn't work," as opposed to TypeError's "this value's TYPE doesn't work."
What you'll practice
hoard = ["gold", "gems"] — only 2 items, at positions 0 and 1. This code asks for position 5, which doesn't exist. Run it, read the IndexError, then fix it to ask for a REAL position — the last item, gems.
More from this topic
Name that exception is one lesson inside Reading Python Error Messages, for Kids — see the full lesson order and what the whole topic covers.
View the full topic →