BugHunt

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

You did arithmetic with a None. Something in the sum was missing and nobody checked.

PythonNull and undefined errors

What it means

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.

Common causes

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.

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