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
- Check the types of both operands: print(type(x), type(y))
- Convert user input or string values to numbers: int(input('Enter number: '))
- Use explicit type conversion before the operation: float(value) + 1.0
- 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.
