TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' in Python means you did arithmetic with a None. Something in the sum was missing and nobody checked.
How do I fix TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'?
To fix TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' in Python, locate the line causing the error and check the values right before it runs. Most occurrences happen because of dict.get() with no default. Apply a defensive check or safe fallback, make sure collections or objects exist before access, and verify return values from upstream functions.
What does TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' mean?
Python refuses to add a number and None rather than guessing. The line that crashes is where the None was used, which is often nowhere near where it was created.
Why does TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' happen?
1. dict.get() with no default
get returns None for a missing key. Give it a default of the right type.
Breaks
total = sum(scores.get(n) for n in names)Works
total = sum(scores.get(n, 0) for n in names)2. A function that returns None on some path
An if with no else silently returns None for the inputs that miss the branch.
Breaks
def bonus(score):
if score > 50:
return 10
total = score + bonus(score)Works
def bonus(score):
if score > 50:
return 10
return 03. Missing data from a database or API
A nullable column comes back as None, and it only fails for the rows that are empty.
Breaks
total = row["base"] + row["extra"]Works
total = row["base"] + (row["extra"] or 0)How to find it in your own code
Give every lookup a typed default rather than letting None travel. Prefer `x is None` over truthiness when checking, because a legitimate 0 is falsy but not missing.
Try it: fix this bug
mediumtotal_score({"a": 5}, ["a", "b"]) crashes: TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'.
def total_score(scores, names):
return sum(scores.get(n) for n in names)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.