BugHunt

TypeError: unsupported operand type(s) for +: 'int' and 'str'

You added a number and a string. Python will not guess whether you meant arithmetic or joining text, so convert one side.

PythonType errors

What it means

+ adds numbers and joins strings, never a mix. The message names the left and right types: an int on the left, a str on the right. Inside sum(), the int is sum's starting value 0 and the str is your data.

Common causes

1. Summing text that looks like numbers

split() returns strings, even when they contain digits.

Breaks

total = sum("4,5,6".split(","))

Works

total = sum(int(v) for v in "4,5,6".split(","))

2. sum() over a dictionary of names

Iterating a dictionary yields its keys.

Breaks

total = sum({"a": 1, "b": 2})

Works

total = sum({"a": 1, "b": 2}.values())

3. Adding form input to a number

Everything from input() or a form is text.

Breaks

total = 0
total += form_value

Works

total = 0
total += int(form_value)

How to find it in your own code

Print type() of both sides on the failing line. Convert text to numbers where it enters the program, with int() or float(), rather than halfway through a calculation.

Try it: fix this bug

medium

total_values({'a': 1, 'b': 2}) raises TypeError: unsupported operand type(s) for +: 'int' and 'str'.

def total_values(data):
    return sum(data)
Open with hintsRuns in your browser. No account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other common errors