Table of Contents
Start debugging a Python error by reading the final line of the traceback: it names the exception and usually explains the immediate problem. Then inspect the indicated line and the values used there. The five examples below show a failing case and a corrected version; run each example separately.
1. IndentationError: a block is not indented
Python uses indentation to group statements. A multiline loop needs an indented body.
# Incorrect
for i in range(3):
print(i)
# Correct
for i in range(3):
print(i)The correct version prints 0, 1 and 2 on separate lines. Configure your editor to use consistent spaces, conventionally four per level. Mixing tabs and spaces can cause a related TabError. A single-line loop such as for i in range(3): print(i) is valid; it is not an example of missing indentation.

2. SyntaxError: Python cannot parse the statement
Look for missing colons, unclosed quotes or brackets, and misspelled keywords. The location where Python detects a problem may be after the actual mistake, so check the previous line too.
# Incorrect: missing colon
if True
print("Ready")
# Correct
if True:
print("Ready")Use = for assignment and == for equality comparison. Fix a syntax error in the source rather than expecting a handler inside that same unparseable script to catch it.

3. IndexError: a sequence index is out of range
numbers = [10, 20, 30]
print(numbers[3]) # IndexError: there is no fourth itemFor a three-item list, non-negative indexes are 0, 1 and 2. Negative indexes count backward, so numbers[-1] is 30. An empty list has no valid element index.
index = 2
if 0 <= index < len(numbers):
print(numbers[index])
else:
print("Choose an index within the list")This check deliberately accepts only non-negative indexes. If you only need values, iterate directly with for number in numbers: instead of calculating indexes. See our Python data-type overview for sequence basics.

4. ValueError: the value is unsuitable
int("ten") raises ValueError because that text cannot be interpreted as an integer. Catch the specific exception when invalid input is an expected possibility.
raw = "ten"
try:
number = int(raw)
except ValueError:
print("Enter an integer, such as 10")
else:
print(number)Another example is math.sqrt(-5), which is outside the real-valued square-root domain. math.sqrt(0) is valid: the function accepts zero as well as positive values. If complex arithmetic is intended, investigate cmath.sqrt rather than silently replacing the input.

5. AttributeError: the object lacks that attribute
text = "hello"
text.push() # AttributeError: str has no push methodCheck the actual object and the method you intended. Strings are immutable; a new string can be formed with text + "!". Lists use methods such as append, but changing a string to a list is only appropriate if your program needs a list.
user = None
if user is None:
print("No user is available")
else:
print(user.name)The guard handles the missing-user case. It does not guarantee that every other object has a name attribute. Trace where the object was created or returned, and use type(user) or dir(user) to inspect it.

A repeatable debugging process
- Read the exception type and message.
- Locate the failing expression and inspect its inputs.
- Reduce the problem to a small example.
- Fix the cause, then check both a normal input and the failing case.
Avoid a broad exception handler that hides every error. Use the official errors-and-exceptions tutorial for handling patterns. If Python does not start at all, use our separate Python command troubleshooting guide.
Reader Comments 0
Sign in with email or Google to join the discussion.