TheAlgorithms/Python · error · ValueError

Parameter number must be greater than or equal to 0

Error message

Parameter number must be greater than or equal to 0

What it means

Raised by digit_factorial_sum() in project_euler/problem_074/sol2.py when number is a negative int. Digit factorial sums are only defined for non-negative integers (the doctest shows 0 -> 1 because 0! == 1), so negatives are rejected with ValueError after the isinstance check passes.

Source

Thrown at project_euler/problem_074/sol2.py:66

        ...
    TypeError: Parameter number must be int

    >>> digit_factorial_sum(-1)
    Traceback (most recent call last):
        ...
    ValueError: Parameter number must be greater than or equal to 0

    >>> digit_factorial_sum(0)
    1

    >>> digit_factorial_sum(69)
    363600
    """
    if not isinstance(number, int):
        raise TypeError("Parameter number must be int")

    if number < 0:
        raise ValueError("Parameter number must be greater than or equal to 0")

    # Converts number in string to iterate on its digits and adds its factorial.
    return sum(DIGIT_FACTORIAL[digit] for digit in str(number))


def solution(chain_length: int = 60, number_limit: int = 1000000) -> int:
    """
    Returns the number of numbers below number_limit that produce chains with exactly
    chain_length non repeating elements.

    >>> solution(10.0, 1000)
    Traceback (most recent call last):
        ...
    TypeError: Parameters chain_length and number_limit must be int

    >>> solution(10, 1000.0)
    Traceback (most recent call last):
        ...

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass number >= 0.
  2. Fix loop bounds so the variable never goes negative.
  3. Clamp or reject negatives before calling: if number < 0: skip/raise.

Example fix

# before
for num in range(start, stop, -1):  # runs past 0
    s = digit_factorial_sum(num)

# after
for num in range(start, -1, -1):
    s = digit_factorial_sum(num)
Defensive patterns

Strategy: validation

Validate before calling

if number < 0:
    raise ValueError(f"number must be >= 0, got {number}")
digit_factorial_sum(number)

Type guard

def is_non_negative_int(value) -> bool:
    return isinstance(value, int) and value >= 0

Try / catch

try:
    s = digit_factorial_sum(num)
except ValueError:
    logger.warning("negative input %d skipped", num)
    continue

Prevention

When it happens

Trigger: digit_factorial_sum(-1), digit_factorial_sum(-50), or forwarding a loop variable that iterates into negatives (e.g. range(n, -1, -1) misuse).

Common situations: Buggy loop bounds in chain-length experiments (Project Euler 74 style); signed differences or deltas passed directly as the number; user input not clamped to >= 0.

Related errors


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