{"record":{"id":"ce3bfde9a7ae6197","repo":"TheAlgorithms/Python","slug":"n-is-negative","errorCode":null,"errorMessage":"n is negative","messagePattern":"n is negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/fibonacci.py","lineNumber":57,"sourceCode":"\ndef fib_iterative_yield(n: int) -> Iterator[int]:\n    \"\"\"\n    Calculates the first n (1-indexed) Fibonacci numbers using iteration with yield\n    >>> list(fib_iterative_yield(0))\n    [0]\n    >>> tuple(fib_iterative_yield(1))\n    (0, 1)\n    >>> tuple(fib_iterative_yield(5))\n    (0, 1, 1, 2, 3, 5)\n    >>> tuple(fib_iterative_yield(10))\n    (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)\n    >>> tuple(fib_iterative_yield(-1))\n    Traceback (most recent call last):\n        ...\n    ValueError: n is negative\n    \"\"\"\n    if n < 0:\n        raise ValueError(\"n is negative\")\n    a, b = 0, 1\n    yield a\n    for _ in range(n):\n        yield b\n        a, b = b, a + b\n\n\ndef fib_iterative(n: int) -> list[int]:\n    \"\"\"\n    Calculates the first n (0-indexed) Fibonacci numbers using iteration\n    >>> fib_iterative(0)\n    [0]\n    >>> fib_iterative(1)\n    [0, 1]\n    >>> fib_iterative(5)\n    [0, 1, 1, 2, 3, 5]\n    >>> fib_iterative(10)\n    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/fibonacci.py#L39-L75","documentation":"Raised by fib_iterative_yield() in maths/fibonacci.py, a generator yielding the first n Fibonacci numbers, when n is negative. There is no meaningful 'first -1 Fibonacci numbers', so the generator raises ValueError on the first next() before yielding anything.","triggerScenarios":"Calling tuple(fib_iterative_yield(-1)) or iterating the generator with any negative n. Because it is a generator, the ValueError surfaces lazily at first iteration, not at call time.","commonSituations":"Computing counts from lengths minus offsets (e.g. range sizes from user pagination input), passing a negative slice-derived count, or defaulting missing numeric config to a negative sentinel.","solutions":["Validate n >= 0 at the call site before consuming the generator.","Fix the arithmetic producing the negative count (typically len(x) - k where k > len(x)).","Treat negative input as 'no terms' explicitly: `tuple(fib_iterative_yield(max(n, 0)))` if that semantics is intended."],"exampleFix":"# before\nn = total - offset  # negative when offset > total\nlist(fib_iterative_yield(n))\n\n# after\nif n < 0:\n    raise ValueError(f'offset {offset} exceeds total {total}')\nlist(fib_iterative_yield(n))","handlingStrategy":"validation","validationCode":"if n < 0:\n    raise ValueError(f'n must be >= 0, got {n}')\nterms = tuple(fib_iterative_yield(n))","typeGuard":null,"tryCatchPattern":"# Generator raises lazily at first next(), so wrap consumption:\ntry:\n    terms = list(fib_iterative_yield(n))\nexcept ValueError as exc:\n    raise ValueError(f'invalid fib count {n}') from exc","preventionTips":["Remember generators raise at iteration time, not call time — validate before consuming.","Check computed counts (len - offset) for negativity before use.","Model 'no terms' as skipping the call, not as a negative count."],"tags":["math","fibonacci","generator","negative-value","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}