donnemartin/interactive-coding-challenges · error · TypeError

val cannot be None

Error message

val cannot be None

What it means

Raised by MathOps.insert (running mean/min/max/mode tracker in math_probability/math_ops) when val is None. Every statistic is updated incrementally on insert, so a None value would corrupt the accumulators; the method rejects it up front.

Source

Thrown at math_probability/math_ops/math_ops_solution.ipynb:118

    "\n",
    "\n",
    "class Solution(object):\n",
    "\n",
    "    def __init__(self, upper_limit=100):\n",
    "        self.max = None\n",
    "        self.min = None\n",
    "        # Mean\n",
    "        self.num_items = 0\n",
    "        self.running_sum = 0\n",
    "        self.mean = None\n",
    "        # Mode\n",
    "        self.array = [0] * (upper_limit + 1)\n",
    "        self.mode_occurrences = 0\n",
    "        self.mode = None\n",
    "\n",
    "    def insert(self, val):\n",
    "        if val is None:\n",
    "            raise TypeError('val cannot be None')\n",
    "        if self.max is None or val > self.max:\n",
    "            self.max = val\n",
    "        if self.min is None or val < self.min:\n",
    "            self.min = val\n",
    "        # Calculate the mean\n",
    "        self.num_items += 1\n",
    "        self.running_sum += val\n",
    "        self.mean = self.running_sum / self.num_items\n",
    "        # Calculate the mode\n",
    "        self.array[val] += 1\n",
    "        if self.array[val] > self.mode_occurrences:\n",
    "            self.mode_occurrences = self.array[val]\n",
    "            self.mode = val"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Filter None values before inserting: for v in values: if v is not None: ops.insert(v)
  2. Fix parsing to skip blank/missing entries (csv empty string, JSON null)
  3. If None is meaningful in your domain, substitute a sentinel (e.g. 0) explicitly

Example fix

# before
for val in raw_values:
    ops.insert(val)  # raw_values contains None for missing cells

# after
for val in raw_values:
    if val is None:
        continue
    ops.insert(val)
Defensive patterns

Strategy: validation

Validate before calling

for v in values:
    if v is None: continue
    ops.insert(v)

Type guard

def is_number(v): return isinstance(v, (int, float))

Try / catch

try:
    ops.insert(v)
except TypeError as e:
    logger.warning('skipping invalid value: %s', e)

Prevention

When it happens

Trigger: Calling math_ops.insert(None), e.g. inserting a value pulled from a sparse dataset, a CSV empty cell parsed as None, or an optional function parameter forwarded unchanged.

Common situations: Data-ingestion loops over files/APIs with missing values; batch loaders not filtering None rows.

Related errors


AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28). Data as JSON: /api/errors/93379004303b7685. Report an issue: GitHub.