TheAlgorithms/Python · error · TypeError

Parameter number must be int

Error message

Parameter number must be int

What it means

Raised by digit_factorial_sum() in project_euler/problem_074/sol2.py when number is not an int instance. The function looks up each decimal digit in the DIGIT_FACTORIAL table via str(number); non-int types (float, str, Decimal) either index wrongly or defeat the isinstance contract, so a TypeError is raised first, before the negative check.

Source

Thrown at project_euler/problem_074/sol2.py:63

    >>> digit_factorial_sum(69.0)
    Traceback (most recent call last):
        ...
    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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass a plain int: digit_factorial_sum(69).
  2. Coerce at the boundary: digit_factorial_sum(int(value)) after confirming value is integral (value == int(value)).
  3. Reject strings explicitly rather than relying on the library's TypeError.

Example fix

# before
value = 69.0
digit_factorial_sum(value)  # TypeError

# after
assert float(value).is_integer()
digit_factorial_sum(int(value))
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(number, int) or isinstance(number, bool):
    raise TypeError(f"number must be int, got {type(number).__name__}")
digit_factorial_sum(number)

Type guard

def is_plain_int(value) -> bool:
    return isinstance(value, int) and not isinstance(value, bool)

Try / catch

try:
    s = digit_factorial_sum(number)
except TypeError:
    s = digit_factorial_sum(int(number))  # only if number was numeric
except ValueError:
    raise  # negative input; fix the caller

Prevention

When it happens

Trigger: digit_factorial_sum("69"), digit_factorial_sum(69.0), digit_factorial_sum(3.14), digit_factorial_sum(None). Booleans pass the isinstance check since bool subclasses int.

Common situations: Feeding values that came through JSON or YAML (whole numbers parsed as float); passing a string of digits by mistake; numpy integer types in some configurations.

Related errors


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