TheAlgorithms/Python · error · ValueError

Input must be positive

Error message

Input must be positive

What it means

Raised by aliquot_sum when input_num is an integer but is zero or negative. Proper divisors are only defined for positive integers (the divisor range 1..n//2 is empty or meaningless otherwise), so the function rejects anything <= 0 after the type check.

Source

Thrown at maths/aliquot_sum.py:39

    >>> 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. Validate n >= 1 before calling; raise or skip for non-positive inputs.
  2. Fix loop bounds that include 0 when iterating candidate numbers.
  3. If negatives must be handled, define your own behavior (e.g. use abs) in a wrapper - the library will not.

Example fix

# before
for n in range(0, limit):
    results.append(aliquot_sum(n))

# after
results = [aliquot_sum(n) for n in range(1, limit)]
Defensive patterns

Strategy: validation

Validate before calling

if n < 1:
    raise ValueError(f'n must be >= 1, got {n}')
result = aliquot_sum(n)

Prevention

When it happens

Trigger: Calling aliquot_sum(0), aliquot_sum(-12), or with a computed value that underflowed to 0. bool False reaches here as 0 and True as 1 because bools pass the isinstance(int) check.

Common situations: Boundary values in loops over ranges that start at 0, parsed input of 0 or negatives not validated earlier, or off-by-one errors in range bounds.

Related errors


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