
This TypeError is the cousin of TypeError: unsupported operand type(s) for +. It means: “You tried to use a math operator (like -, *, /) on data types that don’t support it.”
You cannot subtract a number from a word. In this tutorial, we will look at why this unsupported operand error triggers and how to quickly convert your string data into integers so your math works perfectly.
The Cause
The most common cause is, again, input(). The input() function always gives you a string. Problem Code:
total_score = 100
penalty_str = input("Enter penalty points: ") # User types '10'
# 'total_score' is an INT (100)
# 'penalty_str' is a STR ("10")
final_score = total_score - penalty_str
# CRASH! TypeError: unsupported operand type(s) for -: 'int' and 'str'The Fix: Convert Your Types
You must make sure all your variables are the same, numeric type before you do math. Use int() or float() to convert the string.
total_score = 100
penalty_str = input("Enter penalty points: ")
try:
# THE FIX: Convert the string "10" to the number 10
penalty_int = int(penalty_str)
final_score = total_score - penalty_int
print(f"Final score: {final_score}")
except ValueError:
print("That was not a valid number!")This applies to all math operators: * (multiplication), / (division), and ** (exponent).





