{"record":{"id":"76d8f33332b8716f","repo":"TheAlgorithms/Python","slug":"base-must-be-2","errorCode":null,"errorMessage":"base must be >= 2","messagePattern":"base must be >= 2","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/decimal_to_any.py","lineNumber":69,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: base must be >= 2\n    >>> # a base greater than 36 will error\n    >>> decimal_to_any(34, 37) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: base must be <= 36\n    \"\"\"\n    if isinstance(num, float):\n        raise TypeError(\"int() can't convert non-string with explicit base\")\n    if num < 0:\n        raise ValueError(\"parameter must be positive int\")\n    if isinstance(base, str):\n        raise TypeError(\"'str' object cannot be interpreted as an integer\")\n    if isinstance(base, float):\n        raise TypeError(\"'float' object cannot be interpreted as an integer\")\n    if base in (0, 1):\n        raise ValueError(\"base must be >= 2\")\n    if base > 36:\n        raise ValueError(\"base must be <= 36\")\n    new_value = \"\"\n    mod = 0\n    div = 0\n    while div != 1:\n        div, mod = divmod(num, base)\n        if base >= 11 and 9 < mod < 36:\n            actual_value = ALPHABET_VALUES[str(mod)]\n        else:\n            actual_value = str(mod)\n        new_value += actual_value\n        div = num // base\n        num = div\n        if div == 0:\n            return str(new_value[::-1])\n        elif div == 1:\n            new_value += str(div)","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/decimal_to_any.py#L51-L87","documentation":"Raised by decimal_to_any(num, base) in conversions/decimal_to_any.py:69 when base is 0 or 1. A positional numeral system needs at least 2 distinct digits; base 0 makes divmod(num, 0) divide by zero and base 1 cannot represent numbers positionally, so the library refuses both with a ValueError.","triggerScenarios":"decimal_to_any(7, 0), decimal_to_any(7, 1), or any call where the base is computed from data and degenerates to 0/1 (e.g. base = len(alphabet) with an empty or single-char alphabet).","commonSituations":"Allowing end users to type a base in a CLI/web form without bounds; deriving the base from a variable-width alphabet or a count that can be 0 or 1; defaulting a base parameter to 0 as a sentinel.","solutions":["Clamp/validate the base at your boundary: if not 2 <= base <= 36: raise your own error before calling","If the base comes from user input, parse and range-check it in one step: base = int(input_str); assert 2 <= base <= 36","If a sentinel default is needed, use None and substitute 2/10 instead of 0"],"exampleFix":"# before\nbase = len(digits)  # can be 0 or 1\ndecimal_to_any(num, base)\n\n# after\nbase = len(digits)\nif not 2 <= base <= 36:\n    raise ValueError(f\"unsupported base {base}\")\ndecimal_to_any(num, base)","handlingStrategy":"validation","validationCode":"if not (isinstance(base, int) and 2 <= base <= 36):\n    raise ValueError(f\"base must be an int in 2..36, got {base!r}\")","typeGuard":"def is_supported_base(base: object) -> TypeGuard[int]:\n    return isinstance(base, int) and 2 <= base <= 36","tryCatchPattern":"try:\n    decimal_to_any(num, base)\nexcept ValueError as e:\n    if 'base must be' in str(e):\n        raise ValueError(f\"bad radix {base!r} from config\") from e\n    raise","preventionTips":["Never use 0 or 1 as sentinel defaults for a radix","Validate user-supplied bases with a single 2..36 range check","When deriving a base from len(alphabet), assert the alphabet has at least 2 symbols"],"tags":["python","value-error","radix","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}