{"record":{"id":"cf727852da0d19d9","repo":"TheAlgorithms/Python","slug":"n-must-be-an-integer","errorCode":null,"errorMessage":"n must be an integer","messagePattern":"n must be an integer","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"project_euler/problem_046/sol1.py","lineNumber":90,"sourceCode":"    [5777]\r\n    >>> compute_nums(2)\r\n    [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","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_046/sol1.py#L72-L108","documentation":"Raised by compute_nums() in project_euler/problem_046/sol1.py when n is not an int instance. The function uses isinstance(n, int) strictly, so floats (including whole floats like 10.0), strings, and None all fail, even if they look numeric. Note the exception type is ValueError even though it signals a type problem.","triggerScenarios":"compute_nums(\"5\"), compute_nums(1.1), compute_nums(10.0), compute_nums(None), or compute_nums(numpy.int64(5)) on some builds where the value is not a plain int. Booleans pass because bool subclasses int.","commonSituations":"JSON-parsed arguments (json.loads yields floats for numbers like 1.0); CLI args passed as strings; numpy or pandas integer types flowing into the function; division results (e.g. n = len(x)/2) that are floats.","solutions":["Pass a plain int: compute_nums(2).","Coerce near the call site: compute_nums(int(user_value)) after confirming the value is numeric.","For numpy types, convert explicitly: compute_nums(int(np_value)).","If you control the caller chain, keep n as int end-to-end instead of float intermediate values."],"exampleFix":"# before\nn = json.loads('{\"count\": 2.0}')\" \"[\"count\"]\ncompute_nums(n)  # ValueError: n must be an integer\n\n# after\nn = int(json.loads('{\"count\": 2.0}')\" \"[\"count\"])\ncompute_nums(n)","handlingStrategy":"type-guard","validationCode":"if not isinstance(n, int) or isinstance(n, bool):\n    raise TypeError(f\"n must be int, got {type(n).__name__}\")\ncompute_nums(n)","typeGuard":"def is_plain_int(value) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool)","tryCatchPattern":"try:\n    compute_nums(n)\nexcept ValueError as e:\n    if \"must be an integer\" in str(e):\n        n = int(float(n))  # only if n was numeric\n        compute_nums(n)\n    else:\n        raise","preventionTips":["Convert JSON/YAML numeric fields to int at load time, not at call time.","Remember bool passes isinstance(n, int); exclude it if needed.","Note the type error is reported as ValueError here; match on message, not type, if you branch on it."],"tags":["project-euler","type-check","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}