{"record":{"id":"b96e38e3c4c6ea1b","repo":"TheAlgorithms/Python","slug":"number-must-be-an-integer-b96e38","errorCode":null,"errorMessage":"number must be an integer","messagePattern":"number must be an integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/special_numbers/perfect_number.py","lineNumber":61,"sourceCode":"    >>> perfect(496)\n    True\n    >>> perfect(8128)\n    True\n    >>> perfect(0)\n    False\n    >>> perfect(-1)\n    False\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        print(msg)\n        raise ValueError(msg)\n\n    print(f\"{number} is {'' if perfect(number) else 'not '}a Perfect Number.\")","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/special_numbers/perfect_number.py#L43-L79","documentation":"Raised by perfect(number) in maths/special_numbers/perfect_number.py when the argument is not an int. Unusually, this is a ValueError rather than a TypeError — the library uses ValueError for both type and range problems, so catching TypeError alone will miss it. Inputs <= 0 do not raise; they return False.","triggerScenarios":"Calling perfect(12.34) or perfect('Hello'). bool passes the isinstance(number, int) check (True is treated as 1, False as 0 -> False).","commonSituations":"Calling code that assumes the conventional TypeError for bad types and catches the wrong exception class; float inputs from measurements or JSON.","solutions":["Pass only builtin ints: convert with int() after confirming the value is integral","Catch ValueError (not TypeError) around this function if you must handle bad input","Normalize inputs at your boundary: reject non-integers before calling"],"exampleFix":"// before\ntry:\n    perfect('12')  # ValueError, not TypeError\nexcept TypeError:\n    ...  # never reached\n\n// after\ntry:\n    perfect(int('12'))\nexcept ValueError:\n    ...","handlingStrategy":"type-guard","validationCode":"if not isinstance(n, int) or isinstance(n, bool):\n    raise ValueError('perfect() requires a builtin int')\nprint(perfect(n))","typeGuard":"def is_valid_perfect_input(v) -> TypeGuard[int]:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    perfect(n)\nexcept ValueError as e:  # type errors ALSO come out as ValueError here\n    if 'integer' in str(e):\n        n = int(n)\n    else:\n        raise","preventionTips":["This module raises ValueError for bad types — do not catch TypeError expecting it","bool sneaks past the isinstance check","Convert integral floats with int() before calling"],"tags":["math","valueerror","type-checking","perfect-number","api-quirk"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}