TheAlgorithms/Python · error · ValueError

Invalid numbering system

Error message

Invalid numbering system

What it means

NumberingSystem.max_value raises ValueError('Invalid numbering system') from the match statement's fallback case when the resolved enum member is neither SHORT, LONG, nor INDIAN. In practice an unknown system name (e.g. 'french') raises KeyError earlier at cls[system.upper()], so this ValueError mainly guards against future enum members added without a case — the reachable invalid-input error is the KeyError.

Source

Thrown at conversions/convert_number_to_words.py:52

        """
        Gets the max value supported by the given number system.

        >>> NumberingSystem.max_value("short") == 10**18 - 1
        True
        >>> NumberingSystem.max_value("long") == 10**21 - 1
        True
        >>> NumberingSystem.max_value("indian") == 10**19 - 1
        True
        """
        match system_enum := cls[system.upper()]:
            case cls.SHORT:
                max_exp = system_enum.value[0][0] + 3
            case cls.LONG:
                max_exp = system_enum.value[0][0] + 6
            case cls.INDIAN:
                max_exp = 19
            case _:
                raise ValueError("Invalid numbering system")
        return 10**max_exp - 1


class NumberWords(Enum):
    ONES = {  # noqa: RUF012
        0: "",
        1: "one",
        2: "two",
        3: "three",
        4: "four",
        5: "five",
        6: "six",
        7: "seven",
        8: "eight",
        9: "nine",
    }

    TEENS = {  # noqa: RUF012

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use one of the three accepted names: 'short', 'long', or 'indian'
  2. Catch both KeyError and ValueError when system names come from user input
  3. Validate against [e.name.lower() for e in NumberingSystem] before calling

Example fix

# before
convert_number(1234, 'european')

# after
convert_number(1234, 'long')
Defensive patterns

Strategy: validation

Validate before calling

VALID_SYSTEMS = {'short', 'long', 'indian'}
if system.lower() not in VALID_SYSTEMS:
    raise ValueError(f'system must be one of {sorted(VALID_SYSTEMS)}')

Type guard

def is_valid_system(name) -> bool:
    return isinstance(name, str) and name.upper() in {'SHORT', 'LONG', 'INDIAN'}

Try / catch

try:
    convert_number(n, system)
except (KeyError, ValueError):
    return convert_number(n, 'short')  # fallback system

Prevention

When it happens

Trigger: convert_number(123, 'short') works; convert_number(123, 'metric') raises KeyError('METRIC') from the cls[...] lookup; the ValueError path requires an enum member outside the three cased members.

Common situations: Assuming arbitrary locale names ('us', 'uk', 'european') are accepted; passing the enum member itself instead of its name string; case is handled (.upper()) but aliases are not.

Related errors


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