TheAlgorithms/Python · error · ValueError

base must be <= 36

Error message

base must be <= 36

What it means

Raised by decimal_to_any(num, base) in conversions/decimal_to_any.py:71 when base exceeds 36. The digit alphabet is 0-9 plus A-Z (26 letters), i.e. exactly 36 symbols, so bases above 36 have no representable digit set in this implementation and are rejected with a ValueError.

Source

Thrown at conversions/decimal_to_any.py:71

    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)
            return str(new_value[::-1])

View on GitHub (pinned to f5988cc097)

Solutions

  1. Cap the radix at 36, or use 36 itself if you just need a compact encoding
  2. If you truly need base > 36, switch to a library that supports arbitrary alphabets (e.g. python-baseconv, base62) instead of this function
  3. Range-check user-supplied bases (2..36) before calling decimal_to_any

Example fix

# before
decimal_to_any(user_id, 62)  # ValueError

# after
from baseconv import Base62
Base62.encode(user_id)
Defensive patterns

Strategy: validation

Validate before calling

if not 2 <= base <= 36:
    raise ValueError(f"base {base} unsupported; this library covers 2..36 only")

Try / catch

try:
    decimal_to_any(num, base)
except ValueError as e:
    if 'base must be <= 36' in str(e):
        num = decimal_to_any(num, 36)  # or switch to a base62 library
    else:
        raise

Prevention

When it happens

Trigger: decimal_to_any(34, 37), decimal_to_any(999999, 62) (base64-style radix), or bases taken from specs that assume base62/base64 support which this library does not provide.

Common situations: Porting code that uses base36/base62 URL-shortener IDs and assuming arbitrary radix support; passing a base read from a spec or config that allows > 36; confusing bit-width or alphabet length with a valid radix.

Related errors


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