donnemartin/interactive-coding-challenges · error · ValueError

num cannot be less than one

Error message

num cannot be less than one

What it means

Raised by Solution.fizz_buzz when num < 1. The sequence is defined from 1 to num inclusive, so zero or negative counts are meaningless and rejected with ValueError. Unlike the None check, this is a domain/range validation error.

Source

Thrown at arrays_strings/fizz_buzz/fizz_buzz_solution.ipynb:117

   "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": {},
   "source": [
    "## Unit Test"

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Clamp num to at least 1 if a non-empty result is required (max(1, num))
  2. Reject at the API boundary with a 400-style message for num < 1
  3. Fix the arithmetic that produced a zero/negative count

Example fix

# before
Solution().fizz_buzz(count)  # count may be 0

# after
Solution().fizz_buzz(max(1, count))
Defensive patterns

Strategy: validation

Validate before calling

if num >= 1:
    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 ValueError as e:
    if 'less than one' in str(e):
        num = 1
        Solution().fizz_buzz(num)
    else:
        raise

Prevention

When it happens

Trigger: Calling fizz_buzz(0) or fizz_buzz(-5); computing num from arithmetic that can go non-positive.

Common situations: Pagination/counters decrementing past zero; user-supplied counts not bounded; off-by-one in loop bounds passed as num.

Related errors


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