donnemartin/interactive-coding-challenges · error · TypeError
num cannot be None
Error message
num cannot be None
What it means
Raised by Solution.fizz_buzz when num is None. The method iterates range(1, num+1), so None would fail comparison; the guard rejects it up front with a clear TypeError. num must be a positive integer.
Source
Thrown at arrays_strings/fizz_buzz/fizz_buzz_solution.ipynb:115
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def fizz_buzz(self, num):\n",
" if num is None:\n",
" raise TypeError('num cannot be None')\n",
" if num < 1:\n",
" raise ValueError('num cannot be less than one')\n",
" results = []\n",
" for i in range(1, num + 1):\n",
" if i % 3 == 0 and i % 5 == 0:\n",
" results.append('FizzBuzz')\n",
" elif i % 3 == 0:\n",
" results.append('Fizz')\n",
" elif i % 5 == 0:\n",
" results.append('Buzz')\n",
" else:\n",
" results.append(str(i))\n",
" return results"
]
},
{
"cell_type": "markdown",
"metadata": {},View on GitHub (pinned to 358f2cc604)
Solutions
- Validate and convert input to int before calling (e.g. int(request.args['n']))
- Default None to a sensible value like 0 or 1 depending on desired behavior
- Use argument parsing that enforces a required integer
Example fix
# before
Solution().fizz_buzz(params.get('n'))
# after
n = int(params['n'])
Solution().fizz_buzz(n) Defensive patterns
Strategy: validation
Validate before calling
if num is not None:
Solution().fizz_buzz(num) Type guard
def is_positive_int(num) -> bool:
return isinstance(num, int) and num >= 1 Try / catch
try:
Solution().fizz_buzz(num)
except TypeError:
num = 1 # or return an error to the caller Prevention
- Convert and validate numeric input at the boundary
- Use argparse/validators that require an int
When it happens
Trigger: Calling fizz_buzz(None); passing an unset counter or a value parsed from unvalidated input.
Common situations: CLI/script arguments not converted to int; API handlers forwarding optional query params directly.
Related errors
- data cannot be None
- neighbor or weight cannot be None
- neighbor cannot be None
- key cannot be None
- key cannot be None
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/1adb15563bca35d7.
Report an issue: GitHub.