TheAlgorithms/Python · error · ValueError
number must be an integer
Error message
number must be an integer
What it means
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.
Source
Thrown at maths/special_numbers/perfect_number.py:61
>>> perfect(496)
True
>>> perfect(8128)
True
>>> perfect(0)
False
>>> perfect(-1)
False
>>> perfect(12.34)
Traceback (most recent call last):
...
ValueError: number must be an integer
>>> perfect("Hello")
Traceback (most recent call last):
...
ValueError: number must be an integer
"""
if not isinstance(number, int):
raise ValueError("number must be an integer")
if number <= 0:
return False
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
if __name__ == "__main__":
from doctest import testmod
testmod()
print("Program to check whether a number is a Perfect number or not...")
try:
number = int(input("Enter a positive integer: ").strip())
except ValueError:
msg = "number must be an integer"
print(msg)
raise ValueError(msg)
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")View on GitHub (pinned to f5988cc097)
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
Example fix
// before
try:
perfect('12') # ValueError, not TypeError
except TypeError:
... # never reached
// after
try:
perfect(int('12'))
except ValueError:
... Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(n, int) or isinstance(n, bool):
raise ValueError('perfect() requires a builtin int')
print(perfect(n)) Type guard
def is_valid_perfect_input(v) -> TypeGuard[int]:
return isinstance(v, int) and not isinstance(v, bool) Try / catch
try:
perfect(n)
except ValueError as e: # type errors ALSO come out as ValueError here
if 'integer' in str(e):
n = int(n)
else:
raise Prevention
- 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
When it happens
Trigger: Calling perfect(12.34) or perfect('Hello'). bool passes the isinstance(number, int) check (True is treated as 1, False as 0 -> False).
Common situations: Calling code that assumes the conventional TypeError for bad types and catches the wrong exception class; float inputs from measurements or JSON.
Related errors
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
- surface_area_sphere() only accepts non-negative values
- surface_area_hemisphere() only accepts non-negative values
- surface_area_cone() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b96e38e3c4c6ea1b.
Report an issue: GitHub.