{"record":{"id":"8c376aa5181fee12","repo":"TheAlgorithms/Python","slug":"number-must-be-an-integer-8c376a","errorCode":null,"errorMessage":"number must be an integer","messagePattern":"number must be an integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/perfect_number.py","lineNumber":69,"sourceCode":"    >>> perfect(33550337)  # Just above a large perfect number\n    False\n    >>> perfect(1)  # Edge case: 1 is not a perfect number\n    False\n    >>> perfect(\"123\")  # String representation of a number\n    Traceback (most recent call last):\n    ...\n    ValueError: number must be an integer\n    >>> perfect(12.34)\n    Traceback (most recent call last):\n      ...\n    ValueError: number must be an integer\n    >>> perfect(\"Hello\")\n    Traceback (most recent call last):\n      ...\n    ValueError: number must be an integer\n    \"\"\"\n    if not isinstance(number, int):\n        raise ValueError(\"number must be an integer\")\n    if number <= 0:\n        return False\n    return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n    print(\"Program to check whether a number is a Perfect number or not...\")\n    try:\n        number = int(input(\"Enter a positive integer: \").strip())\n    except ValueError:\n        msg = \"number must be an integer\"\n        raise ValueError(msg)\n\n    print(f\"{number} is {'' if perfect(number) else 'not '}a Perfect Number.\")\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/perfect_number.py#L51-L87","documentation":"perfect() in maths/perfect_number.py decides whether a number equals the sum of its proper divisors (sum(i for i in range(1, number // 2 + 1) ...) == number). It guards its input with isinstance(number, int) and raises ValueError when the argument is not a Python int, because the divisor-sum loop and number % i comparisons assume exact integer arithmetic. Note the exception type is ValueError, not TypeError, even though it is a type problem — matching the doctest contract of the module.","triggerScenarios":"Calling perfect(12.34), perfect('Hello'), perfect(6.0), or passing an unconverted value from input()/file parsing directly to perfect().","commonSituations":"Reading a number from a config file or API payload and passing it through without int() conversion; wrapping the function in generic code that catches TypeError but not ValueError, so the guard slips through.","solutions":["Convert to int at the call site: perfect(int(number)) when the value is known to be integral.","Parse user input explicitly (int(input().strip()) inside try/except ValueError) instead of passing raw strings.","If you wrap calls in error handling, catch ValueError (not TypeError) — that is what this function raises."],"exampleFix":"# before\nperfect(user_value)  # ValueError if user_value is 6.0 or '6'\n\n# after\nperfect(int(user_value))","handlingStrategy":"type-guard","validationCode":"if not isinstance(number, int):\n    number = int(number)  # only after confirming it is numeric\nresult = perfect(number)","typeGuard":"def is_int_like(v) -> bool:\n    return isinstance(v, int) or (isinstance(v, str) and v.lstrip('-').isdigit())","tryCatchPattern":"try:\n    perfect(n)\nexcept ValueError as exc:\n    if 'integer' in str(exc):\n        n = int(float(n))  # or reject\n    else:\n        raise","preventionTips":["Catch ValueError, not TypeError — this module raises ValueError for type problems.","Centralize int() conversion where data enters your program.","Check the function's doctest block for the exact error contract."],"tags":["python","value-error","input-validation","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}