The dragon explains
Name that exception
The dragon: You've met NameError. There are a few more crash TYPES worth recognising on sight — each one is a different diagnosis.
The dragon: IndexError means: you asked a LIST for a position that doesn't exist.
example.pyPython 3hoard = ["gold", "gems"]print(hoard[5])
OutputIndexError: list index out of rangeThe dragon: KeyError means: you asked a DICT for a key that was never set.
example.pyPython 3dragon = {"name": "Ember"}print(dragon["mana"])
OutputKeyError: 'mana'The dragon: TypeError means: you tried to combine two things that don't mix — most often, text and a number joined with +.
example.pyPython 3print("Level: " + 5)OutputTypeError: can only concatenate str (not "int") to strThe dragon: ValueError means: you tried to CONVERT text that was never a real number in the first place.
example.pyPython 3int('abc')OutputValueError: invalid literal for int() with base 10: 'abc'The dragon: ZeroDivisionError means exactly what it says: dividing by zero, which has no real answer.
example.pyPython 3print(10 / 0)OutputZeroDivisionError: division by zeroThe dragon: Five names, five different symptoms. Once you recognise the TYPE, you already know roughly where to look — before reading a single word of the message.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 2
party = ["Ember", "Blaze"]. print(party[9]) raises which error?
question.pyPython 3party = ["Ember", "Blaze"]print(party[9])
- IndexError — position 9 doesn't exist in a 2-item listCorrect
- KeyError — 9 isn't a valid key
- TypeError — 9 is the wrong type for indexing
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.
Question 2 of 2
score = int("high"). What error does this raise, and why?
question.pyPython 3score = int("high")- ValueError — 'high' isn't made of digits, so there's nothing to convertCorrect
- TypeError — int() was given the wrong type
- NameError — 'high' was never defined
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."
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
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.
- +2
PracticeTwo more, on your own
This lesson is part of PRO.
PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.
Unlock this lesson with PROStuck three times? The game offers the answer — unasked.