🔧 Error Fixes

Fix: Python TypeError — unsupported operand type(s)


TypeError: unsupported operand type(s) for +: 'int' and 'str'

You’re trying to use an operator on incompatible types.

Fix 1: Convert types explicitly

# ❌ Can't add int + str
age = 30
message = "Age: " + age  # TypeError!

# ✅ Convert to string
message = "Age: " + str(age)

# ✅ Or use f-string (best)
message = f"Age: {age}"

Fix 2: Check input types

# ❌ input() returns a string, not a number
num = input("Enter a number: ")
result = num + 10  # TypeError!

# ✅ Convert to int
num = int(input("Enter a number: "))
result = num + 10

Fix 3: Check for None

# ❌ Function returned None instead of a number
def get_price(item):
    if item == "apple":
        return 1.50
    # Missing return for other items → returns None

total = get_price("banana") + 5  # TypeError: None + int

# ✅ Handle the None case
price = get_price("banana")
if price is not None:
    total = price + 5

Fix 4: Check data from APIs/files

# JSON data might have unexpected types
data = json.loads(response.text)
# data['price'] might be a string "9.99" instead of float 9.99

total = float(data['price']) + tax

Debugging tip

# Check the types
print(type(a), type(b))

See also: Python cheat sheet