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





