
This error is the opposite of the common string concatenation error. It means you tried to do math (+), but one of your items was a string. If you see a TypeError unsupported operand type message, it’s likely because the operands involved aren’t of compatible types for the operation you’ve tried.
Problem Code:
age = 25 age_in_five_years = age + "5" # CRASH! # TypeError: unsupported operand type(s) for +: 'int' and 'str'
Python says: “I know how to add int + int (math). I know how to add str + str (joining). I don’t know how to add int + str.”
โก Quick Fix: TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ โ Python int() and float() Conversion Fix for input() Math Operations
You mixed a number and a string with a math operator โ Python’s + does either math or text joining, never both at once, and it refuses to guess which one you meant.
# WRONG โ "5" is a string, you can't add it to an integer
age = 25
age_in_five_years = age + "5" # TypeError: unsupported operand type(s) for +: 'int' and 'str'
# WRONG โ input() always returns a string, even when the user types a number
age = input("Enter your age: ") # user types 25 โ age is still the string "25"
age_in_five_years = age + 5 # TypeError fires here
# RIGHT โ convert the string to int before doing math
age_string = input("Enter your age: ")
age_int = int(age_string)
age_in_five_years = age_int + 5
print(age_in_five_years) # Output: 30
# RIGHT โ production pattern: wrap int() in try/except for unpredictable input
try:
age = int(input("Enter your age: "))
print(f"In 5 years you'll be {age + 5}")
except ValueError:
print("Please enter a whole number.")The breakdown below covers the hardcoded string case and the input() case โ the two sources that trigger this error in 99% of Python scripts.
The Cause (User Input)
This happens 99% of the time with the input() function. input() always returns a string, even if the user types a number.
age = input("Enter your age: ") # User types '25'
age_in_five_years = age + 5 # CRASH! (age is the string "25")The Fix: Convert to a Number
You must explicitly convert the string to an integer (int) or float (float) before you can do math with it.
age_string = input("Enter your age: ")
age_int = int(age_string) # Convert the string "25" to the number 25
# Now it works!
age_in_five_years = age_int + 5
print(age_in_five_years)Pro Tip: Wrap your int() conversion in a try/except block to catch cases where the user types “hello” instead of “25”.
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ โ The Type Conversion Rule Every Python Developer Needs
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ enforces one rule: every value touching a math operator must be a number. Python never converts types silently โ you do it explicitly or it crashes.
input() is the single biggest source of this error. It returns a string every time, regardless of what the user types. Wrap every input() that feeds a math operation with int() or float() at the point of assignment โ not later in the code where the crash lands.
Three conversion functions cover every case.
int() converts whole number strings: int(“25”) gives 25. It crashes on decimals โ int(“25.5”) raises ValueError. Use it for ages, counts, menu choices, and any value that must be a whole number.
float() converts decimal strings: float(“25.5”) gives 25.5. It also handles whole numbers: float(“25”) gives 25.0. Use it for prices, measurements, and any value that might carry a decimal.
int(float()) handles strings that arrive as decimals but need to be integers: int(float(“25.5”)) gives 25. This pattern covers CSV data, API responses, and scraped values where the format isn’t guaranteed.
Wrap the conversion in try/except ValueError on any input you don’t fully control. A user who types “hello”, a CSV cell with “N/A”, or an API field returning null all crash bare int() or float() calls. The try/except pattern costs three lines and eliminates the entire class of conversion crashes permanently.
try:
value = int(float(input(“Enter a number: “).strip()))
except ValueError:
print(“Invalid input โ enter a number.”)





