TheAlgorithms/Python · error · ValueError

area_trapezium() only accepts non-negative values

Error message

area_trapezium() only accepts non-negative values

What it means

area_trapezium() raises this ValueError when base1, base2, or height is negative. The library validates all three dimensions before returning 1/2 * (base1 + base2) * height. Note that the two bases may be passed in either order — only negativity, not ordering, is rejected.

Source

Thrown at maths/area.py:431

    >>> area_trapezium(1, 2, -3)
    Traceback (most recent call last):
        ...
    ValueError: area_trapezium() only accepts non-negative values
    >>> area_trapezium(-1, -2, 3)
    Traceback (most recent call last):
        ...
    ValueError: area_trapezium() only accepts non-negative values
    >>> area_trapezium(1, -2, -3)
    Traceback (most recent call last):
        ...
    ValueError: area_trapezium() only accepts non-negative values
    >>> area_trapezium(-1, 2, -3)
    Traceback (most recent call last):
        ...
    ValueError: area_trapezium() only accepts non-negative values
    """
    if base1 < 0 or base2 < 0 or height < 0:
        raise ValueError("area_trapezium() only accepts non-negative values")
    return 1 / 2 * (base1 + base2) * height


def area_circle(radius: float) -> float:
    """
    Calculate the area of a circle.

    >>> area_circle(20)
    1256.6370614359173
    >>> area_circle(1.6)
    8.042477193189871
    >>> area_circle(0)
    0.0
    >>> area_circle(-1)
    Traceback (most recent call last):
        ...
    ValueError: area_circle() only accepts non-negative values
    """

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check min(base1, base2, height) >= 0 before the call and repair the negative dimension.
  2. For depth-like heights measured against a datum, subtract in a fixed direction or use abs().
  3. Sanitize data at import time.
  4. Catch ValueError and name the offending parameter.

Example fix

// before
area = area_trapezium(b1, b2, h)  # h = surface - datum may be negative

# after
h = abs(surface - datum)
area = area_trapezium(b1, b2, h)
Defensive patterns

Strategy: validation

Validate before calling

if base1 < 0 or base2 < 0 or height < 0:
    raise ValueError(f'trapezium dimensions must be >= 0: {base1}, {base2}, {height}')
area = area_trapezium(base1, base2, height)

Type guard

def is_valid_trapezium(b1: float, b2: float, h: float) -> bool:
    return min(b1, b2, h) >= 0

Try / catch

try:
    area = area_trapezium(base1, base2, height)
except ValueError as e:
    raise ValueError(f'trapezium input rejected: {e}') from e

Prevention

When it happens

Trigger: Calling area_trapezium(base1, base2, height) with any argument < 0, e.g. area_trapezium(-1, 2, -3) or area_trapezium(1, -2, -3).

Common situations: Cross-section dimensions in earthwork/volume calculations where a depth goes negative when terrain is above a datum; spreadsheet imports with negative placeholders; argument-order experiments that pass signed deltas.

Related errors


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