TheAlgorithms/Python · error · ValueError

Input must be an integer

Error message

Input must be an integer

What it means

Raised by aliquot_sum in maths/aliquot_sum.py when input_num is not an int (e.g. 1.6 or a string). The function sums proper divisors via the modulo operator, which is only meaningful for integers, so non-integers are rejected before the divisor scan.

Source

Thrown at maths/aliquot_sum.py:37

      ...
    ValueError: Input must be positive
    >>> aliquot_sum(0)
    Traceback (most recent call last):
      ...
    ValueError: Input must be positive
    >>> aliquot_sum(1.6)
    Traceback (most recent call last):
      ...
    ValueError: Input must be an integer
    >>> aliquot_sum(12)
    16
    >>> aliquot_sum(1)
    0
    >>> aliquot_sum(19)
    1
    """
    if not isinstance(input_num, int):
        raise ValueError("Input must be an integer")
    if input_num <= 0:
        raise ValueError("Input must be positive")
    return sum(
        divisor for divisor in range(1, input_num // 2 + 1) if input_num % divisor == 0
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to int first if the value is integral: aliquot_sum(int(x)) after verifying x is whole.
  2. Validate upstream input types before the numeric pipeline.
  3. Reject bools explicitly if they can occur: isinstance(x, bool) check before passing.

Example fix

# before
result = aliquot_sum(user_value)  # user_value is 12.0 from JSON

# after
if not (isinstance(user_value, int) and not isinstance(user_value, bool)):
    user_value = int(user_value)
result = aliquot_sum(user_value)
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(user_value, int) or isinstance(user_value, bool):
    if not float(user_value).is_integer():
        raise TypeError('value must be a whole number')
    user_value = int(user_value)
result = aliquot_sum(user_value)

Type guard

def is_whole_int(value) -> bool:
    return isinstance(value, int) and not isinstance(value, bool)

Prevention

When it happens

Trigger: Calling aliquot_sum(1.6), aliquot_sum('12'), or with a numpy float. Note: Python bool passes this check because bool subclasses int - True is treated as 1.

Common situations: Values arriving from JSON parsing or user input as floats (12.0 fails too, since 12.0 is a float not an int), or unconverted string parameters from CLI args.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/869c53a22b8a4d95. Report an issue: GitHub.