{"record":{"id":"724cef7a2e89d6fa","repo":"TheAlgorithms/Python","slug":"k-must-be-an-integer","errorCode":null,"errorMessage":"k must be an integer.","messagePattern":"k must be an integer\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"searches/fibonacci_search.py","lineNumber":47,"sourceCode":"        Fibonacci number in position k.\n\n    >>> fibonacci(0)\n    0\n    >>> fibonacci(2)\n    1\n    >>> fibonacci(5)\n    5\n    >>> fibonacci(15)\n    610\n    >>> fibonacci('a')\n    Traceback (most recent call last):\n    TypeError: k must be an integer.\n    >>> fibonacci(-5)\n    Traceback (most recent call last):\n    ValueError: k integer must be greater or equal to zero.\n    \"\"\"\n    if not isinstance(k, int):\n        raise TypeError(\"k must be an integer.\")\n    if k < 0:\n        raise ValueError(\"k integer must be greater or equal to zero.\")\n    if k == 0:\n        return 0\n    elif k == 1:\n        return 1\n    else:\n        return fibonacci(k - 1) + fibonacci(k - 2)\n\n\ndef fibonacci_search(arr: list, val: int) -> int:\n    \"\"\"A pure Python implementation of a fibonacci search algorithm.\n\n    Parameters\n    ----------\n    arr\n        List of sorted elements.\n    val","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/searches/fibonacci_search.py#L29-L65","documentation":"Raised by the recursive fibonacci helper in searches/fibonacci_search.py when k is not an int. fibonacci_search uses Fibonacci numbers to pick probe offsets, and list indexing with a non-integer offset would be meaningless, so the helper hard-rejects non-int inputs with TypeError. Because isinstance(k, int) is the test, floats like 5.0 are rejected even though they are integer-valued, and bool passes (True/False are int subclasses).","triggerScenarios":"fibonacci('a'); fibonacci(10.0); fibonacci_search internals are safe because they only pass ints, so this error almost always comes from calling the helper directly with a value parsed from JSON/config (where numbers arrive as float or string).","commonSituations":"json.load produces floats for values like 10.0; CLI args passed as strings; numpy scalar types (np.int64 is fine on most builds but np.float64 is not an int).","solutions":["Pass a plain Python int: fibonacci(10).","Coerce at the boundary: fibonacci(int(k)) after confirming no data loss (e.g. k == int(k)).","For values loaded from JSON, convert with int() before calling the helper."],"exampleFix":"# before\nfibonacci(float(user_input))  # e.g. 15.0 -> TypeError\n\n# after\nfibonacci(int(float(user_input)))  # or int(user_input) for plain digit strings","handlingStrategy":"type-guard","validationCode":"if not isinstance(k, int) or isinstance(k, bool):\n    k = int(k)  # or raise TypeError\nfibonacci(k)","typeGuard":"def is_int_index(k) -> bool:\n    return isinstance(k, int) and not isinstance(k, bool)","tryCatchPattern":"try:\n    fib_k = fibonacci(k)\nexcept TypeError:\n    fib_k = fibonacci(int(k))","preventionTips":["Convert JSON/CLI values to int at the boundary of your program.","Remember bool passes isinstance(x, int); exclude it explicitly if it matters.","Large k in this naive recursion is exponential — memoize before worrying about types."],"tags":["search","fibonacci","type-error","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}