Python TypeError: unsupported operand type(s)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
This error appears when the values on the two sides of an operator are a pair that the operation does not work on. The error message tells you exactly what is wrong: what follows for is the operator, and the two words after it are the types of the left and right values. for +: 'int' and 'str' means you tried to do integer + string.
Cause 1. You added a string to a number
Values read with input() and split() are strings, even when they look like numbers. Adding one straight to a total of 0 is integer + string.
Wrong code
total = 0for x in input().split(): total = total + xprint(total)3 5 7
Traceback (most recent call last): File "main.py", line 3, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str'
Fixed code
Convert with int() before adding. To convert all the numbers on a line at once, use map(int, ...).
total = 0for x in map(int, input().split()): total = total + xprint(total)3 5 7
15
Cause 2. You subtracted or divided strings
+ between two strings joins them, so it runs without an error ("7" + "2" is "72"). But - and / are operations strings do not have.
Wrong code
a = input()b = input()print(a - b)7 2
Traceback (most recent call last): File "main.py", line 3, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'str'
Fixed code
Convert both to numbers first, then calculate.
Cause 3. You calculated with None
If you see 'NoneType' where a type should be, one of the values is None (no value). A function with no return returns None. This happens when you use the result of a function that only calculates and does not return, or that uses print instead of return.
Wrong code
def double(x): x * 2 print(double(3) + 1)Traceback (most recent call last): File "main.py", line 4, in <module> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Fixed code
Make the function give the value back with return.
Cause 4. You put a whole list into a calculation
You cannot divide a whole list by a number, or subtract a number from it. This often happens when you type the list's name instead of the variable holding the total.
Wrong code
scores = [80, 90, 100]print(scores / len(scores))Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: unsupported operand type(s) for /: 'list' and 'int'
Fixed code
To calculate with the values in a list, add them up with sum(), or take them out one by one in a loop.
scores = [80, 90, 100]print(sum(scores) / len(scores))print([s // 10 for s in scores])90.0 [8, 9, 10]
What to check when you see this error
- Read the operator after
forand the two types in the error message.'str'is a string,'int'is an integer,'list'is a list, and'NoneType'isNone. - On the line where the error happened (the number after
line), find that operator and check that the left and right values match the order of the types in the error message. - If you see
'str', check that you converted the value frominput()withint()orfloat(). - If you see
'NoneType', check that the function that produced the value has areturn.
Practice problems
See also
- Python TypeError: can only concatenate str
- Python ValueError: invalid literal for int()
- Python 'NoneType' object has no attribute
- Syntax: Python input(): Read User Input
- Syntax: Python split(): Split a String into a List
- Learn it step by step: Python Reading input
- All Python error messages
- Online Python interpreter