donnemartin/interactive-coding-challenges · error · TypeError

num cannot be None

Error message

num cannot be None

What it means

Bits.flip_bit raises TypeError when num is None. The method finds the longest run of 1s achievable by flipping one bit and immediately performs bitwise operations on num, so None is rejected up front.

Source

Thrown at bit_manipulation/flip_bit/flip_bit_solution.ipynb:138

    "    MAX_BITS = 32\n",
    "    \n",
    "    def _build_seen_list(self, num):\n",
    "        seen = []\n",
    "        looking_for = 0\n",
    "        count = 0\n",
    "        for _ in range(self.MAX_BITS):\n",
    "            if num & 1 != looking_for:\n",
    "                seen.append(count)\n",
    "                looking_for = not looking_for\n",
    "                count = 0\n",
    "            count += 1\n",
    "            num >>= 1\n",
    "        seen.append(count)\n",
    "        return seen\n",
    "    \n",
    "    def flip_bit(self, num):\n",
    "        if num is None:\n",
    "            raise TypeError('num cannot be None')\n",
    "        if num == -1:\n",
    "            return self.MAX_BITS\n",
    "        if num == 0:\n",
    "            return 1\n",
    "        seen = self._build_seen_list(num)\n",
    "        max_result = 0\n",
    "        looking_for = 0\n",
    "        for index, count in enumerate(seen):\n",
    "            result = 0\n",
    "            # Only look for zeroes\n",
    "            if looking_for == 1:\n",
    "                looking_for = not looking_for\n",
    "                continue\n",
    "            # First iteration, take trailing zeroes\n",
    "            # or trailing ones into account\n",
    "            if index == 0:\n",
    "                if count != 0:\n",
    "                    # Trailing zeroes\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Ensure num is an int before calling; flip_bit(num or 0) when 0 is an acceptable default
  2. Fix the upstream parse/lookup that produced None
  3. Catch TypeError defensively if inputs are untrusted

Example fix

# before
result = bits.flip_bit(user_value)

# after
if user_value is None:
    raise ValueError('user_value is required')
result = bits.flip_bit(user_value)
Defensive patterns

Strategy: validation

Validate before calling

if num is None:
    raise ValueError('num is required')
Bits().flip_bit(num)

Type guard

def is_int(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool)

Try / catch

try:
    result = bits.flip_bit(num)
except TypeError:
    result = 1  # default for missing input

Prevention

When it happens

Trigger: Bits().flip_bit(None), or flip_bit(num) where num is an optional parameter, a failed int() parse, or a missing dict/config value.

Common situations: Numeric input parsed from strings (int(x) guarded by try/except that left None), optional numeric fields, or test scaffolding that forgot to set the value.

Related errors


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