{"record":{"id":"b1ff12d90ff6ba18","repo":"TheAlgorithms/Python","slug":"base-must-be-between-2-and-36-inclusive","errorCode":null,"errorMessage":"'base' must be between 2 and 36 inclusive","messagePattern":"'base' must be between 2 and 36 inclusive","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/special_numbers/harshad_numbers.py","lineNumber":38,"sourceCode":"    >>> int_to_base(167, 16)\n    'A7'\n    >>> # bases below 2 and beyond 36 will error\n    >>> int_to_base(98, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: 'base' must be between 2 and 36 inclusive\n    >>> int_to_base(98, 37)\n    Traceback (most recent call last):\n        ...\n    ValueError: 'base' must be between 2 and 36 inclusive\n    >>> int_to_base(-99, 16)\n    Traceback (most recent call last):\n        ...\n    ValueError: number must be a positive integer\n    \"\"\"\n\n    if base < 2 or base > 36:\n        raise ValueError(\"'base' must be between 2 and 36 inclusive\")\n\n    digits = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n    result = \"\"\n\n    if number < 0:\n        raise ValueError(\"number must be a positive integer\")\n\n    while number > 0:\n        number, remainder = divmod(number, base)\n        result = digits[remainder] + result\n\n    if result == \"\":\n        result = \"0\"\n\n    return result\n\n\ndef sum_of_digits(num: int, base: int) -> str:","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/special_numbers/harshad_numbers.py#L20-L56","documentation":"Raised by int_to_base() in maths/special_numbers/harshad_numbers.py when the base argument is outside [2, 36]. The function converts a positive integer to its string representation in the given base using the digit alphabet '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', which only defines 36 digits, so bases below 2 (no positional radix) or above 36 (no digit symbols) are meaningless.","triggerScenarios":"Calling int_to_base(number, base) with base < 2 (e.g. base=1 or base=0) or base > 36 (e.g. base=37). The check runs before any conversion, so even valid numbers like int_to_base(98, 37) fail immediately.","commonSituations":"Passing a user-supplied radix without clamping, off-by-one loops like `for b in range(2, 37)` miswritten as `range(2, 38)`, or assuming the function supports arbitrary bases like base64.","solutions":["Clamp or reject the base before calling: only call when 2 <= base <= 36","If you need base-2-only logic, hard-code base=2 instead of parameterizing","Validate user input for base at the API boundary with a clear error of your own"],"exampleFix":"// before\ns = int_to_base(98, 37)  # ValueError\n\n// after\nbase = 37\nif not 2 <= base <= 36:\n    raise ValueError(f\"unsupported base {base}\")\ns = int_to_base(98, base)","handlingStrategy":"validation","validationCode":"def safe_int_to_base(number: int, base: int) -> str:\n    if not 2 <= base <= 36:\n        raise ValueError(f\"base {base} out of range 2-36\")\n    return int_to_base(number, base)","typeGuard":null,"tryCatchPattern":"try:\n    s = int_to_base(n, base)\nexcept ValueError as e:\n    if 'base' in str(e):\n        s = int_to_base(n, 10)  # fall back to decimal\n    else:\n        raise","preventionTips":["Validate base once at your boundary and reuse the validated value","Use range(2, 37) when sweeping bases","Write a unit test for base=1 and base=37 boundaries"],"tags":["math","input-validation","valueerror","radix"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}