Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

5 Common Python Errors and How to Fix Them

Diagnose IndentationError, SyntaxError, IndexError, ValueError and AttributeError with correctly formatted Python examples and specific fixes.

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.

Title SEO: 5 Common Python Errors and How to Fix Them for Beginners Picture 1

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.

Title SEO: 5 Common Python Errors and How to Fix Them for Beginners Picture 2

3. IndexError: a sequence index is out of range

numbers = [10, 20, 30]
print(numbers[3])  # IndexError: there is no fourth item

For 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.

Title SEO: 5 Common Python Errors and How to Fix Them for Beginners Picture 3

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.

Title SEO: 5 Common Python Errors and How to Fix Them for Beginners Picture 4

5. AttributeError: the object lacks that attribute

text = "hello"
text.push()  # AttributeError: str has no push method

Check 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.

Title SEO: 5 Common Python Errors and How to Fix Them for Beginners Picture 5

A repeatable debugging process

  1. Read the exception type and message.
  2. Locate the failing expression and inspect its inputs.
  3. Reduce the problem to a small example.
  4. 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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.