Pro Tips to Spot Errors Quickly
-
ValueError → "The value you gave is wrong" ✅ → Right type, wrong data
-
IndexError → "The position you're trying to reach doesn’t exist" ❌
-
TypeError → "You're mixing incompatible types" ⚡
-
NameError → "That variable doesn’t exist" 🔍
-
SyntaxError → "Python can’t even read this code" 🛑
-------------------------------------------------------------------------------------------------------------------
| Error Type --------------- |
Meaning -------------------- |
Cause ------------------------- |
Example ------------------------ |
How to Spot / Fix ----------------------- |
|---|---|---|---|---|
| SyntaxError | Invalid code structure | Python cannot parse your code | print "Hello" |
Check for missing colons, parentheses, quotes |
| IndentationError | Wrong indentation level | Misaligned spaces/tabs | if True:\nprint("Hi") |
Always use consistent indentation (4 spaces) |
| NameError | Variable not defined | Using a variable before declaring it | print(x) |
Define the variable before use |
| TypeError | Incompatible types | Performing invalid operations between data types | "5" + 5 |
Convert types explicitly: int("5") + 5 |
| ValueError | Invalid value for the operation | Correct type but unacceptable value | int("abc") |
Ensure input values are valid |
| IndexError | Index out of range | Accessing an invalid list/tuple index | [1,2,3][5] |
Check list length before accessing |
| KeyError | Missing dictionary key | Accessing a non-existent key | {"a":1}["b"] |
Use .get() or check with in |
| ZeroDivisionError | Division by zero | Dividing a number by zero | 10/0 |
Check denominator before division |
| AttributeError | Invalid attribute access | Using a method or property that doesn't exist | "abc".push() |
Use dir(obj) to see valid attributes |
| ImportError | Module not found | Importing a non-existent or misplaced module | import mymodule (not present) |
Check module name and path |
| ModuleNotFoundError | Subclass of ImportError | Specific case when the module cannot be located | import unknownlib |
Install or correct module |
| RuntimeError | Error that occurs at runtime | General unexpected errors | Rare and contextual | Check traceback for details |
| AssertionError | Failed assert statement |
When the assertion condition is false | assert 2+2==5 |
Validate assumptions |
| OverflowError | Number too large for calculation | Exceeding numeric limits | math.exp(1000) |
Use decimal or fractions module |
| MemoryError | Not enough memory | Processing huge datasets | Creating a massive list | Optimize memory usage |
| EOFError | Input ends unexpectedly | Calling input() but no data provided |
input() without data |
Ensure valid user input |
| OSError | System-related errors | Issues like missing files, permissions, etc. | open("nofile.txt") |
Check file paths and permissions |