TheAlgorithms/Python · error · ValueError

All values must be greater than 0

Error message

All values must be greater than 0

What it means

Raised by check_polygon() in maths/check_polygon.py when any value in the input list is <= 0. Side lengths of a polygon must be strictly positive for the polygon-inequality check (largest side < sum of the rest) to be meaningful; a zero or negative 'length' is geometrically invalid, so the function raises ValueError.

Source

Thrown at maths/check_polygon.py:35

    >>> check_polygon([1, 4.3, 5.2, 12.2])
    False
    >>> nums = [3, 7, 13, 2]
    >>> _ = check_polygon(nums) #   Run function, do not show answer in output
    >>> nums #  Check numbers are not reordered
    [3, 7, 13, 2]
    >>> check_polygon([])
    Traceback (most recent call last):
        ...
    ValueError: Monogons and Digons are not polygons in the Euclidean space
    >>> check_polygon([-2, 5, 6])
    Traceback (most recent call last):
        ...
    ValueError: All values must be greater than 0
    """
    if len(nums) < 2:
        raise ValueError("Monogons and Digons are not polygons in the Euclidean space")
    if any(i <= 0 for i in nums):
        raise ValueError("All values must be greater than 0")
    copy_nums = nums.copy()
    copy_nums.sort()
    return copy_nums[-1] < sum(copy_nums[:-1])


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Filter or reject non-positive values before calling: all(s > 0 for s in sides).
  2. Take absolute values only if negative values are genuinely signed measurements and magnitude is what you meant.
  3. Fix data entry / parsing so side lengths are always positive reals.

Example fix

# before
check_polygon([-2, 5, 6])  # ValueError

# after
sides = [abs(s) for s in [-2, 5, 6]]
if any(s <= 0 for s in sides):
    raise ValueError('side lengths must be positive')
check_polygon(sides)
Defensive patterns

Strategy: validation

Validate before calling

if not nums or any(s <= 0 for s in nums):
    raise ValueError(f'side lengths must be positive: {nums}')

Type guard

def are_positive_sides(nums) -> bool:
    return len(nums) >= 3 and all(isinstance(s, (int, float)) and s > 0 for s in nums)

Try / catch

try:
    check_polygon(sides)
except ValueError as e:
    if 'greater than 0' in str(e):
        sides = [abs(s) for s in sides if s != 0]  # only if magnitudes were intended
    else:
        raise

Prevention

When it happens

Trigger: Calling check_polygon([-2, 5, 6]) or check_polygon([0, 4, 4]) — any list containing a zero or negative number. The guard is any(i <= 0 for i in nums).

Common situations: Signed distances or deltas passed where magnitudes were intended; sensor data with zeros/NaNs represented as 0; typos such as -2 instead of 2; unvalidated user or CSV input with missing values parsed as negative sentinels.

Related errors


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