{"record":{"id":"f92843e9e4c48f62","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-greater-than-or-equal-to-one","errorCode":null,"errorMessage":"Parameter n must be greater than or equal to one.","messagePattern":"Parameter n must be greater than or equal to one\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"project_euler/problem_002/sol4.py","lineNumber":62,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: Parameter n must be greater than or equal to one.\n    >>> solution([])\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    >>> solution(\"asd\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    \"\"\"\n\n    try:\n        n = int(n)\n    except TypeError, ValueError:\n        raise TypeError(\"Parameter n must be int or castable to int.\")\n    if n <= 0:\n        raise ValueError(\"Parameter n must be greater than or equal to one.\")\n    getcontext().prec = 100\n    phi = (Decimal(5) ** Decimal(\"0.5\") + 1) / Decimal(2)\n\n    index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2\n    num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2)\n    total = num // 2\n    return int(total)\n\n\nif __name__ == \"__main__\":\n    print(f\"{solution() = }\")\n","sourceCodeStart":44,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_002/sol4.py#L44-L74","documentation":"Documented ValueError from project_euler/problem_002/sol4.py:solution when n <= 0 after successful int conversion. The Fibonacci-derived formula needs a positive upper bound. CAVEAT: the surrounding try/except uses Python 2 syntax ('except TypeError, ValueError:'), so under Python 3 the module dies with SyntaxError at import and this ValueError is unreachable until the syntax is fixed.","triggerScenarios":"After fixing the syntax: solution(0), solution(-5), or solution('0') (string casts fine, then fails the > 0 check). As written today: importing the module under Python 3 raises SyntaxError before this check can ever run.","commonSituations":"Passing 0 as a 'no limit' sentinel; off-by-one bugs producing 0 from max(k-1, 0)-style computations; empty-input edge cases in a generic harness that calls every Project Euler solution with n=0; running under Python 3 where the file's py2 except syntax breaks first.","solutions":["Fix 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' at project_euler/problem_002/sol4.py:60 so the module imports at all.","Pass n >= 1 (the smallest valid Fibonacci-even-sum bound).","Pre-validate n >= 1 at the call site for harness code that iterates over many n values.","Catch ValueError to skip invalid bounds in batch runs."],"exampleFix":"# before\nsolution(0)\n\n# after\nif n >= 1:\n    result = solution(n)","handlingStrategy":"validation","validationCode":"n = int(n)\nif n <= 0:\n    raise ValueError('n must be >= 1')\nsolution(n)","typeGuard":"def is_valid_fib_bound(n) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 1","tryCatchPattern":"try:\n    solution(n)\nexcept ValueError as e:\n    if 'greater than or equal to one' in str(e):\n        ...","preventionTips":["Never pass 0 as a 'no bound' sentinel to this API.","Clamp sweep ranges to start at 1.","Fix the py2 except syntax first or the module will not import under Python 3."],"tags":["python-euler","python2-syntax","valueerror","validation","fibonacci"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}