ProjectStack
python

TypeError: unsupported operand type(s)

You used a mathematical or comparison operator between two types that don't support that operation together. For example, adding a string to an integer or subtracting a list from a number.

Common causes

  • Performing arithmetic on mixed types: '5' + 3 or None - 1
  • Comparing types that can't be compared with <, >, etc. in Python 3
  • A variable holds an unexpected type — a string where a number was expected
  • User input (which is always a string) used directly in arithmetic

How to fix it

  1. Check the types of both operands: print(type(x), type(y))
  2. Convert user input or string values to numbers: int(input('Enter number: '))
  3. Use explicit type conversion before the operation: float(value) + 1.0
  4. Add input validation to ensure variables hold the expected type before operating on them

Example

Traceback (most recent call last): File "app.py", line 3, in <module> total = price + tax TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

Adding a price (which is None because a lookup failed) to a float tax value

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.