Lesson 19 of 19 · 2 min
Common Errors & Fixes
| Error Message | Why it happens | How to fix |
|---|---|---|
TypeError: function_name() missing 1 required positional argument | Called a function without providing all the arguments it needs | Check the def line and supply every required argument when calling |
UnboundLocalError: local variable 'x' referenced before assignment | A function tried to modify a global variable without declaring it with global | Add global x as the first line inside the function |
IndexError: list index out of range | Tried to access a position in a list that does not exist (e.g. index 5 in a 3-item list) | Check len(the_list) before accessing, or use negative indexes like [-1] for the last item |
KeyError: 'some_key' | Tried to access a dictionary key that does not exist | Use .get(key) which returns None safely, or check if key in dict first |
AttributeError: 'dict' object has no attribute 'add' | Used {} to create an empty set — but {} actually makes an empty dictionary | Use set() to create an empty set, never {} |
TypeError: 'tuple' object does not support item assignment | Tried to change an item inside a tuple (e.g. coords[0] = 5) — tuples are immutable | Use a list [] instead if the data needs to change; or create a new tuple |
