{"record":{"id":"5c8d9b967afe5610","repo":"TheAlgorithms/Python","slug":"k-integer-must-be-greater-or-equal-to-zero","errorCode":null,"errorMessage":"k integer must be greater or equal to zero.","messagePattern":"k integer must be greater or equal to zero\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"searches/fibonacci_search.py","lineNumber":49,"sourceCode":"    >>> 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\n        Element to search in list.\n","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/searches/fibonacci_search.py#L31-L67","documentation":"Raised by the fibonacci helper in searches/fibonacci_search.py when k is a negative integer. The helper defines Fibonacci numbers for indices 0 and 1 and recurses downward, so negative indices have no base case and would recurse forever (until RecursionError); the guard rejects them up front with ValueError. It fires only after the isinstance(k, int) check, so the value is a genuine negative int.","triggerScenarios":"fibonacci(-5); fibonacci(-1); passing a loop variable that underflows below zero, e.g. a while k >= 0 loop rewritten incorrectly.","commonSituations":"Index arithmetic that goes negative during searches; validating untrusted numeric input without a lower bound; porting code from languages where negative indexing is meaningful.","solutions":["Only call with k >= 0; index 0 returns 0 and index 1 returns 1.","Clamp or reject at your boundary: if k < 0: raise ValueError(...).","If negative Fibonacci (Fibonacci extension) is actually wanted, use an explicit closed-form/Binet implementation, not this helper."],"exampleFix":"# before\nfibonacci(n - 2)  # n = 1 -> fibonacci(-1) -> ValueError\n\n# after\nif n - 2 < 0:\n    return n  # base case handled by caller\nfibonacci(n - 2)","handlingStrategy":"validation","validationCode":"if k < 0:\n    raise ValueError(f'k must be >= 0, got {k}')\nfibonacci(k)","typeGuard":null,"tryCatchPattern":"try:\n    fib_k = fibonacci(k)\nexcept ValueError:\n    fib_k = fibonacci(abs(k))  # only if negative indexing is semantically wrong input","preventionTips":["Check lower bounds on index arithmetic before calling helpers.","Loop conditions like `while k >= 0` must not let the body run with k == -1.","Never rely on RecursionError as the 'error message' for negative inputs; validate explicitly."],"tags":["search","fibonacci","precondition","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}