TheAlgorithms/Python · error · TypeError

Input value of [number={number}] must be an integer

Error message

Input value of [number={number}] must be an integer

What it means

Raised by catalan_number() in maths/special_numbers/catalan_number.py when number is not an int. The Catalan numbers are computed with exact integer arithmetic (multiplication and floor division in a loop), so floats — even whole-valued ones like 5.0 — are rejected with TypeError. Type checking runs before the range check, so 5.0 raises this error, not the '> 0' one.

Source

Thrown at maths/special_numbers/catalan_number.py:35

    >>> catalan(5)
    14
    >>> catalan(0)
    Traceback (most recent call last):
        ...
    ValueError: Input value of [number=0] must be > 0
    >>> catalan(-1)
    Traceback (most recent call last):
        ...
    ValueError: Input value of [number=-1] must be > 0
    >>> catalan(5.0)
    Traceback (most recent call last):
        ...
    TypeError: Input value of [number=5.0] must be an integer
    """

    if not isinstance(number, int):
        msg = f"Input value of [number={number}] must be an integer"
        raise TypeError(msg)

    if number < 1:
        msg = f"Input value of [number={number}] must be > 0"
        raise ValueError(msg)

    current_number = 1

    for i in range(1, number):
        current_number *= 4 * i - 2
        current_number //= i + 1

    return current_number


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use floor division for computed indices: catalan_number(n // 2)
  2. Convert at the boundary: catalan_number(int(user_value))
  3. Coerce whole floats: int(x) if float(x).is_integer() else raise

Example fix

# before
catalan_number(count / 2)  # float -> TypeError

# after
catalan_number(count // 2)  # int
Defensive patterns

Strategy: type-guard

Validate before calling

number = int(number) if isinstance(number, float) and number.is_integer() else number
print(catalan_number(number))

Type guard

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

Try / catch

try:
    catalan_number(number)
except TypeError:
    number = int(number)
    result = catalan_number(number)

Prevention

When it happens

Trigger: Calling catalan_number(5.0), catalan_number('10'), or passing any non-int. Bools pass isinstance but bool True == 1 is a valid index. Division results (x/2) are the most common accidental floats.

Common situations: Counts derived from len()/2 style expressions; JSON/config values typed as floats; string inputs not converted. Note catalan_number(1) == 1 is the smallest valid input.

Related errors


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