donnemartin/interactive-coding-challenges · error · ValueError

license_key must not be empty

Error message

license_key must not be empty

What it means

Raised by Solution.format_license_key when license_key is an empty (or falsy) string. Formatting an empty key is meaningless, so after the None check the method rejects empty input with ValueError before attempting to build the formatted result.

Source

Thrown at online_judges/license_key/format_license_key_solution.ipynb:122

   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "\n",
    "    def format_license_key(self, license_key, k):\n",
    "        if license_key is None:\n",
    "            raise TypeError('license_key must be a str')\n",
    "        if not license_key:\n",
    "            raise ValueError('license_key must not be empty')\n",
    "        formatted_license_key = []\n",
    "        num_chars = 0\n",
    "        for char in license_key[::-1]:\n",
    "            if char == '-':\n",
    "                continue\n",
    "            num_chars += 1\n",
    "            formatted_license_key.append(char.upper())\n",
    "            if num_chars >= k:\n",
    "                formatted_license_key.append('-')\n",
    "                num_chars = 0\n",
    "        if formatted_license_key and formatted_license_key[-1] == '-':\n",
    "            formatted_license_key.pop(-1)\n",
    "        return ''.join(formatted_license_key[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Validate non-empty input before calling: if not key: raise/return early
  2. Trim and require content: key = key.strip(); require key
  3. Provide a default key when the input source may be blank

Example fix

# before
key = request.args.get('key', '')
solution.format_license_key(key, 4)

# after
key = request.args.get('key', '').strip()
if not key:
    return 'key is required', 400
solution.format_license_key(key, 4)
Defensive patterns

Strategy: validation

Validate before calling

license_key = license_key.strip() if license_key else ''
if not license_key: raise ValueError('license_key is required')
solution.format_license_key(license_key, k)

Type guard

def is_non_empty_str(s): return isinstance(s, str) and bool(s.strip())

Try / catch

try:
    key = solution.format_license_key(raw, k)
except ValueError:
    return 'license key required', 400

Prevention

When it happens

Trigger: Calling format_license_key('', 4) or with any falsy string — typically from an empty form field, blank config value, or a stripped input that became ''.

Common situations: Web form handlers passing raw user input; config values present but blank; tests covering the empty-string edge case.

Related errors


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