TheAlgorithms/Python · error · TypeError

dynamic_lucas_number accepts only integer arguments.

Error message

dynamic_lucas_number accepts only integer arguments.

What it means

Raised by dynamic_lucas_number in maths/lucas_series.py when n_th_number is not an int. The dynamic version iterates 'for _ in range(n_th_number)', and range() itself only accepts ints; the explicit isinstance guard gives a clearer error message (naming the function) than range's TypeError would. It computes L(n) by folding a, b = b, a + b starting from 2, 1.

Source

Thrown at maths/lucas_series.py:51

def dynamic_lucas_number(n_th_number: int) -> int:
    """
    Returns the nth lucas number
    >>> dynamic_lucas_number(1)
    1
    >>> dynamic_lucas_number(20)
    15127
    >>> dynamic_lucas_number(0)
    2
    >>> dynamic_lucas_number(25)
    167761
    >>> dynamic_lucas_number(-1.5)
    Traceback (most recent call last):
        ...
    TypeError: dynamic_lucas_number accepts only integer arguments.
    """
    if not isinstance(n_th_number, int):
        raise TypeError("dynamic_lucas_number accepts only integer arguments.")
    a, b = 2, 1
    for _ in range(n_th_number):
        a, b = b, a + b
    return a


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    n = int(input("Enter the number of terms in lucas series:\n").strip())
    print("Using recursive function to calculate lucas series:")
    print(" ".join(str(recursive_lucas_number(i)) for i in range(n)))
    print("\nUsing dynamic function to calculate lucas series:")
    print(" ".join(str(dynamic_lucas_number(i)) for i in range(n)))

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to int at the call site: dynamic_lucas_number(int(n)).
  2. Additionally enforce n >= 0 yourself, since negative ints silently return 2.
  3. Use this function instead of recursive_lucas_number for n above ~30 to avoid exponential runtime.

Example fix

// before
val = dynamic_lucas_number(count)  # count is 20.0

// after
val = dynamic_lucas_number(int(count)) if count >= 0 else None
Defensive patterns

Strategy: type-guard

Validate before calling

n = int(n_th_number)
if n < 0:
    raise ValueError(f"Lucas index must be >= 0, got {n}")
val = dynamic_lucas_number(n)

Type guard

def is_nonneg_int(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 0

Try / catch

try:
    val = dynamic_lucas_number(n)
except TypeError:
    val = dynamic_lucas_number(int(n))

Prevention

When it happens

Trigger: Calling dynamic_lucas_number(-1.5) or dynamic_lucas_number(3.0). The isinstance check fires immediately. Negative ints pass the guard and simply return 2 (range of a negative is empty), which may silently mask a bug.

Common situations: Float counters from progress calculations; reusing the same input across the recursive and dynamic variants and assuming identical validation; expecting negative indices to be rejected (they are not).

Related errors


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