TryBugHunt

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.

PythonPattern: Null and undefined errors

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.

1. Inspect bounds & typesPrint or inspect collection sizes and verify variables are not empty or None/undefined.
2. Guard with safe accessUse optional chaining (?.), dictionary get(), or guard conditions before dereferencing.
3. Trace upstream originsFind where the value was instantiated or returned rather than only patching the symptom.

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 0

3. 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

medium

total_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)
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.