TheAlgorithms/Python · error · ValueError

base must be >= 2

Error message

base must be >= 2

What it means

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.

Source

Thrown at conversions/decimal_to_any.py:69

    Traceback (most recent call last):
        ...
    ValueError: base must be >= 2
    >>> # a base greater than 36 will error
    >>> decimal_to_any(34, 37) # doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    ValueError: base must be <= 36
    """
    if isinstance(num, float):
        raise TypeError("int() can't convert non-string with explicit base")
    if num < 0:
        raise ValueError("parameter must be positive int")
    if isinstance(base, str):
        raise TypeError("'str' object cannot be interpreted as an integer")
    if isinstance(base, float):
        raise TypeError("'float' object cannot be interpreted as an integer")
    if base in (0, 1):
        raise ValueError("base must be >= 2")
    if base > 36:
        raise ValueError("base must be <= 36")
    new_value = ""
    mod = 0
    div = 0
    while div != 1:
        div, mod = divmod(num, base)
        if base >= 11 and 9 < mod < 36:
            actual_value = ALPHABET_VALUES[str(mod)]
        else:
            actual_value = str(mod)
        new_value += actual_value
        div = num // base
        num = div
        if div == 0:
            return str(new_value[::-1])
        elif div == 1:
            new_value += str(div)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp/validate the base at your boundary: if not 2 <= base <= 36: raise your own error before calling
  2. If the base comes from user input, parse and range-check it in one step: base = int(input_str); assert 2 <= base <= 36
  3. If a sentinel default is needed, use None and substitute 2/10 instead of 0

Example fix

# before
base = len(digits)  # can be 0 or 1
decimal_to_any(num, base)

# after
base = len(digits)
if not 2 <= base <= 36:
    raise ValueError(f"unsupported base {base}")
decimal_to_any(num, base)
Defensive patterns

Strategy: validation

Validate before calling

if not (isinstance(base, int) and 2 <= base <= 36):
    raise ValueError(f"base must be an int in 2..36, got {base!r}")

Type guard

def is_supported_base(base: object) -> TypeGuard[int]:
    return isinstance(base, int) and 2 <= base <= 36

Try / catch

try:
    decimal_to_any(num, base)
except ValueError as e:
    if 'base must be' in str(e):
        raise ValueError(f"bad radix {base!r} from config") from e
    raise

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/76d8f33332b8716f. Report an issue: GitHub.