{ }codelper

Error Messages › Python

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)
Input
3 5 7
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Edit and run it →

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)
Input
3 5 7
Output
15

Edit and run it →

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)
Input
7
2
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Edit and run it →

Fixed code

Convert both to numbers first, then calculate.

a = int(input())b = int(input())print(a - b)
Input
7
2
Output
5

Edit and run it →

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)
When you run it
Traceback (most recent call last):
  File "main.py", line 4, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Edit and run it →

Fixed code

Make the function give the value back with return.

def double(x):    return x * 2 print(double(3) + 1)
Output
7

Edit and run it →

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))
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'int'

Edit and run it →

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])
Output
90.0
[8, 9, 10]

Edit and run it →

What to check when you see this error

  1. Read the operator after for and the two types in the error message. 'str' is a string, 'int' is an integer, 'list' is a list, and 'NoneType' is None.
  2. 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.
  3. If you see 'str', check that you converted the value from input() with int() or float().
  4. If you see 'NoneType', check that the function that produced the value has a return.

Practice problems

See also