Common Python Errors: IndexError, KeyError, TypeError Explained

Recognise the common crash types by name, the way a symptom points to a diagnosis.

  • Lesson 2 of 4
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Name that exception

  1. The dragon: You've met NameError. There are a few more crash TYPES worth recognising on sight — each one is a different diagnosis.

  2. The dragon: IndexError means: you asked a LIST for a position that doesn't exist.

    example.pyPython 3
    hoard = ["gold", "gems"]print(hoard[5])
    OutputIndexError: list index out of range
  3. The dragon: KeyError means: you asked a DICT for a key that was never set.

    example.pyPython 3
    dragon = {"name": "Ember"}print(dragon["mana"])
    OutputKeyError: 'mana'
  4. The dragon: TypeError means: you tried to combine two things that don't mix — most often, text and a number joined with +.

    example.pyPython 3
    print("Level: " + 5)
    OutputTypeError: can only concatenate str (not "int") to str
  5. The dragon: ValueError means: you tried to CONVERT text that was never a real number in the first place.

    example.pyPython 3
    int('abc')
    OutputValueError: invalid literal for int() with base 10: 'abc'
  6. The dragon: ZeroDivisionError means exactly what it says: dividing by zero, which has no real answer.

    example.pyPython 3
    print(10 / 0)
    OutputZeroDivisionError: division by zero
  7. The 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.

  1. Question 1 of 2

    party = ["Ember", "Blaze"]. print(party[9]) raises which error?

    question.pyPython 3
    party = ["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.

  2. Question 2 of 2

    score = int("high"). What error does this raise, and why?

    question.pyPython 3
    score = 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. 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. +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 PRO

Stuck three times? The game offers the answer — unasked.