{"record":{"id":"f23503a164a27d0d","repo":"TheAlgorithms/Python","slug":"n-must-be-0","errorCode":null,"errorMessage":"n must be >= 0","messagePattern":"n must be >= 0","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"project_euler/problem_046/sol1.py","lineNumber":92,"sourceCode":"    [5777, 5993]\r\n    >>> compute_nums(0)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: n must be >= 0\r\n    >>> compute_nums(\"a\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: n must be an integer\r\n    >>> compute_nums(1.1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: n must be an integer\r\n\r\n    \"\"\"\r\n    if not isinstance(n, int):\r\n        raise ValueError(\"n must be an integer\")\r\n    if n <= 0:\r\n        raise ValueError(\"n must be >= 0\")\r\n\r\n    list_nums = []\r\n    for num in range(len(odd_composites)):\r\n        i = 0\r\n        while 2 * i * i <= odd_composites[num]:\r\n            rem = odd_composites[num] - 2 * i * i\r\n            if is_prime(rem):\r\n                break\r\n            i += 1\r\n        else:\r\n            list_nums.append(odd_composites[num])\r\n            if len(list_nums) == n:\r\n                return list_nums\r\n\r\n    return []\r\n\r\n\r\ndef solution() -> int:\r","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_046/sol1.py#L74-L110","documentation":"Raised by compute_nums() in project_euler/problem_046/sol1.py when n <= 0. The check 'if n <= 0' rejects both zero and negatives, but the message says 'n must be >= 0', so the message is misleading: passing 0 also raises even though 0 satisfies the stated constraint. Effectively the function requires n >= 1.","triggerScenarios":"compute_nums(0) (raises despite the message implying 0 is allowed), compute_nums(-3). Any caller that clamps or defaults a count to 0 (e.g. max(0, len(results))) will trip this.","commonSituations":"Empty-input handling where a caller legitimately computes a count of 0 (empty list) and forwards it; default parameter values of 0; iterating ranges that start at 0.","solutions":["Pass n >= 1, since 0 is actually rejected by the guard.","Short-circuit zero counts before calling: if n == 0: return [] (or equivalent) instead of calling compute_nums.","If you must handle 0, wrap the call: result = [] if n == 0 else compute_nums(n).","Upstream, treat the message as a doc bug and code against the real contract n >= 1."],"exampleFix":"# before\nn = len(matches)  # can be 0\nnums = compute_nums(n)  # ValueError: n must be >= 0\n\n# after\nnums = compute_nums(n) if n > 0 else []","handlingStrategy":"validation","validationCode":"if n < 1:\n    if n == 0:\n        result = []  # zero-count short circuit\n    else:\n        raise ValueError(f\"n must be >= 1, got {n}\")\nelse:\n    result = compute_nums(n)","typeGuard":"def is_valid_count(n) -> bool:\n    return isinstance(n, int) and n >= 1  # real contract, despite message","tryCatchPattern":"try:\n    nums = compute_nums(n)\nexcept ValueError as e:\n    if str(e) == \"n must be >= 0\":\n        nums = []  # treat zero/negative request as empty result\n    else:\n        raise","preventionTips":["Code against the real contract (n >= 1); the error message is misleading about 0.","Short-circuit zero counts before calling instead of relying on the exception.","Watch for max(0, x) or empty-collection len() defaults feeding this parameter."],"tags":["project-euler","validation","valueerror","off-by-one"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}