Python Errors
50 errors
Common Python errors and how to fix them.
50 / 50
- 1
SyntaxError: invalid syntax
Python can't parse your code — there's a missing colon, parenthesis, or wrong keyword somewhere on the reported line.
- 2
NameError: name 'x' is not defined
Python doesn't recognize the name you used — it was never assigned, imported, or defined in the current scope.
- 3
TypeError: unsupported operand type(s)
You used a value with an operator or function that doesn't accept that type — like adding a string to an integer.
- 4
IndentationError: expected an indented block
Python uses indentation to define code blocks — wrong, missing, or inconsistent indentation breaks the structure.
- 5
IndexError: list index out of range
You tried to access a list element at an index that is beyond the list's valid range (0 to `len(list) - 1`).
- 6
KeyError: 'x'
You tried to get a value from a dictionary using a key that isn't in the dictionary.
- 7
ModuleNotFoundError: No module named 'x'
Python can't find the module you're trying to import — it's not installed, not in the Python path, or the name is wrong.
- 8
AttributeError: 'x' object has no attribute 'y'
You tried to use a method or property on an object that doesn't have it — usually a typo or the wrong object type.
- 9
ValueError: invalid literal for int() with base 10
You passed a value of the correct type but with an invalid content — like converting a non-numeric string to int.
- 10
ZeroDivisionError: division by zero
You attempted to divide a number by zero, which is mathematically undefined.
- 11
FileNotFoundError: [Errno 2] No such file or directory
You tried to open, read, or access a file that doesn't exist at the specified path.
- 12
StopIteration
You called next() on an iterator that has no more values to produce.
- 13
RecursionError: maximum recursion depth exceeded
Your recursive function exceeded Python's maximum call stack depth (default ~1000), usually due to missing or incorrect base case.
- 14
OverflowError: math range error
A calculation produced a number too large for Python to represent, often in float operations.
- 15
UnicodeDecodeError: 'utf-8' codec can't decode byte
You tried to decode bytes using an encoding (like UTF-8) that doesn't match the actual byte content.
- 16
UnicodeEncodeError: 'ascii' codec can't encode character
You tried to encode a string to an encoding that can't represent some of its characters.
- 17
PermissionError: [Errno 13] Permission denied
You tried to read, write, or execute a file or resource without the necessary permissions.
- 18
AssertionError
An assert statement checked a condition that evaluated to false, indicating a programming bug.
- 19
NotImplementedError
You called a method or function that is defined but not yet implemented — a placeholder that needs to be filled in.
- 20
RuntimeError
An error occurred during execution that doesn't fit a more specific exception category.
- 21
UnboundLocalError: local variable 'x' referenced before assignment
You referenced a local variable before assigning a value to it, usually due to a conditional assignment path.
- 22
TabError: inconsistent use of tabs and spaces in indentation
Your file mixes tabs and spaces for indentation in a way that Python can't unambiguously interpret.
- 23
ValueError: not enough values to unpack
You tried to unpack a sequence into a number of variables that doesn't match the sequence's length.
- 24
MemoryError
Your program tried to allocate more memory than is available.
- 25
OSError: [Errno 28] No space left on device
An operating system error occurred — the specific subtype (FileNotFoundError, PermissionError, etc.) gives more detail.
- 26
ConnectionError
A network connection could not be established or was lost — the server may be down or unreachable.
- 27
TimeoutError
A network or I/O operation exceeded its time limit and was terminated.
- 28
json.decoder.JSONDecodeError: Expecting value
You tried to parse a string as JSON, but the string isn't valid JSON format.
- 29
ImportError: cannot import name 'X' from 'Y'
You tried to import a name from a module, but the name doesn't exist in that module or there's a circular import.
- 30
DeprecationWarning
Your code uses a deprecated feature that still works but will be removed or changed in a future Python release.
- 31
RecursionError: maximum recursion depth exceeded
Your recursive function never stops calling itself — Python hits the default recursion depth limit (usually 1000) and raises RecursionError.
- 32
FileNotFoundError: no such file or directory
You tried to open a file that does not exist at the given path — Python raises FileNotFoundError with the path it could not find.
- 33
PermissionError: operation not permitted
You tried to read, write, or execute a file your process does not have permission for — Python raises PermissionError.
- 34
StopIteration from manual next() call
You called next() on an iterator that has no more items — Python raises StopIteration, which crashes your program if uncaught.
- 35
KeyError: key not found in dictionary
You used dict[key] with a key that is not in the dictionary — Python raises KeyError with the missing key as the argument.
- 36
AttributeError: object has no attribute
You tried to access obj.attribute or call obj.method() where the attribute or method does not exist on that object type.
- 37
ValueError: invalid literal for int()
You called int(), float(), or similar with a string that cannot be parsed as a number — Python raises ValueError with the invalid input.
- 38
IndentationError: unexpected indent
Your code has inconsistent indentation — Python uses indentation to define blocks and raises IndentationError when spaces do not align.
- 39
TabError: inconsistent use of tabs and spaces
Your file uses both tabs and spaces for indentation within the same block — Python cannot determine the indentation level and raises TabError.
- 40
SyntaxError: f-string expression cannot include backslash
You used a backslash inside the curly braces of an f-string expression — Python 3.11 and earlier rejects this with a SyntaxError.
- 41
SyntaxError: invalid syntax with walrus operator
You used the walrus operator := in a context Python does not support, or on Python 3.7 or earlier where it is not available.
- 42
SyntaxError: invalid syntax with match statement
You used a match/case statement on Python 3.9 or earlier — structural pattern matching requires Python 3.10+.
- 43
SyntaxError: decorator without function or class
You wrote @decorator but the next line is not a def or class — Python expects a function or class definition after a decorator.
- 44
Mutable default argument bug
You used def f(x=[]) or def f(x={}) — the default is created once at definition time and shared across all calls that use the default.
- 45
Integer division truncation bug
You used // and got an unexpected result because floor division rounds toward negative infinity, not toward zero like truncation.
- 46
is vs == comparison confusion
You used 'a is b' when you meant 'a == b' — 'is' checks if two variables point to the same object, not if their values are equal.
- 47
Shallow copy leaves nested references shared
You used copy.copy() and modified a nested object — both the original and copy share the same nested reference, so both are affected.
- 48
Off-by-one error in range() loop
Your loop iterates one too few or one too many times because range() excludes the stop value — range(5) gives 0..4, not 0..5.
- 49
String immutability: method returns new string
You called s.upper() or s.replace() without assigning the result — strings are immutable, so the original is unchanged.
- 50
Floating-point precision: 0.1 + 0.2 != 0.3
You compared floats with == and got False because 0.1 + 0.2 produces 0.30000000000000004, not exactly 0.3.
No errors found.