{"record":{"id":"031dba8539f99f94","repo":"TheAlgorithms/Python","slug":"recursive-lucas-number-accepts-only-integer-argume","errorCode":null,"errorMessage":"recursive_lucas_number accepts only integer arguments.","messagePattern":"recursive_lucas_number accepts only integer arguments\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"maths/lucas_series.py","lineNumber":23,"sourceCode":"\ndef recursive_lucas_number(n_th_number: int) -> int:\n    \"\"\"\n    Returns the nth lucas number\n    >>> recursive_lucas_number(1)\n    1\n    >>> recursive_lucas_number(20)\n    15127\n    >>> recursive_lucas_number(0)\n    2\n    >>> recursive_lucas_number(25)\n    167761\n    >>> recursive_lucas_number(-1.5)\n    Traceback (most recent call last):\n        ...\n    TypeError: recursive_lucas_number accepts only integer arguments.\n    \"\"\"\n    if not isinstance(n_th_number, int):\n        raise TypeError(\"recursive_lucas_number accepts only integer arguments.\")\n    if n_th_number == 0:\n        return 2\n    if n_th_number == 1:\n        return 1\n\n    return recursive_lucas_number(n_th_number - 1) + recursive_lucas_number(\n        n_th_number - 2\n    )\n\n\ndef dynamic_lucas_number(n_th_number: int) -> int:\n    \"\"\"\n    Returns the nth lucas number\n    >>> dynamic_lucas_number(1)\n    1\n    >>> dynamic_lucas_number(20)\n    15127\n    >>> dynamic_lucas_number(0)","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/lucas_series.py#L5-L41","documentation":"Raised by recursive_lucas_number in maths/lucasas_series.py (maths/lucas_series.py) when n_th_number is not an int. The naive doubly-recursive Lucas implementation (L(0)=2, L(1)=1, L(n)=L(n-1)+L(n-2)) decrements its argument by 1 and 2 each step, which only terminates for integer arguments; the type guard raises TypeError before the first recursive call. Note there is no negative-argument guard, so negative ints recurse until a TypeError is eventually raised by hitting a non-int.","triggerScenarios":"Calling recursive_lucas_number(-1.5), recursive_lucas_number(2.0), or recursive_lucas_number('5'). Also indirectly: calling with a negative int recurses down past 0 until n_th_number - 2 produces a non-int transition point and eventually trips the isinstance check.","commonSituations":"Float indices from formulas; string input from prompts (the __main__ block does int(input()) correctly, but reuse elsewhere may not); passing a negative index expecting generalized Lucas numbers.","solutions":["Convert to non-negative int before calling: recursive_lucas_number(int(n)).","Prefer dynamic_lucas_number for larger n (the recursive version is exponential-time).","Guard n >= 0 at the call site since negative ints are not properly handled."],"exampleFix":"// before\nval = recursive_lucas_number(idx)  # idx is 20.0\n\n// after\nidx = int(idx)\nval = recursive_lucas_number(idx) if idx >= 0 else None","handlingStrategy":"validation","validationCode":"n = int(n_th_number)\nif n < 0:\n    raise ValueError(f\"Lucas index must be >= 0, got {n}\")\nval = dynamic_lucas_number(n)  # prefer dynamic for speed","typeGuard":"def is_nonneg_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":"try:\n    val = recursive_lucas_number(n)\nexcept TypeError:\n    val = dynamic_lucas_number(int(n))","preventionTips":["Always enforce n >= 0 yourself; the recursive variant mishandles negative ints","Use the dynamic variant for n above ~30 to avoid exponential runtime"],"tags":["math","lucas","recursion","typeerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}