Python Errors

Lessons Python Book Chapter 6
Python Book · Chapter 6

Playing Through Errors

Errors aren’t interruptions — they’re landmarks. Every blank window, crash, freeze, or traceback is a clue that tells you exactly where the edge of the map is. This chapter teaches you how to read those clues and keep playing.

Chapter 6 of 6

Errors are part of the game.

When something breaks, it means you changed something meaningful. You touched a boundary. You poked the system. That’s progress — not failure.

Python’s error messages look intimidating at first, but they’re actually incredibly helpful once you know how to read them.


Understanding tracebacks.

When Python hits an error, it prints a “traceback.” This is a breadcrumb trail showing exactly where things went wrong.

Traceback (most recent call last):
  File “drifting_stars.py”, line 42, in <module>
    star[0] += star[2]
TypeError: unsupported operand type(s) for +=: ‘str’ and ‘float’

This tells you three things:

  • Which file broke
  • Which line broke
  • Why it broke

In this case, a value that should be a number became a string — probably because of a tweak. Easy fix.


Common errors you’ll see (and why they happen).

1. NameError — Python can’t find a variable.

NameError: name ‘speed’ is not defined

Usually a typo or a variable used before it exists.

2. TypeError — You mixed types that don’t go together.

TypeError: unsupported operand type(s)

Happens when a number becomes a string or vice‑versa.

3. IndexError — You tried to access something outside a list.

IndexError: list index out of range

Happens when tweaking star or ball arrays.

4. Pygame error — Usually means the window closed early.

pygame.error: display Surface quit

Happens when the loop keeps running after the window closes.


How to fix errors without fear.

1. Read the last line first.
That’s the actual error.

2. Go to the line number.
Python tells you exactly where to look.

3. Undo your last tweak.
If the error disappears, you found the cause.

4. Change one thing at a time.
This is the heart of the Play‑First Loop.

5. Ask AI for help.
Copy the traceback and paste it into your AI assistant. It will explain the error in plain English.


Break something on purpose.

Open any of your previous projects and intentionally cause an error. Delete a bracket. Change a type. Remove a variable. Then fix it.

This is how you build intuition — not by avoiding errors, but by dancing with them.

You finished Python Book · Chapter 6 of 6
>