TheAlgorithms/Python · error · ValueError
area_parallelogram() only accepts non-negative values
Error message
area_parallelogram() only accepts non-negative values
What it means
area_parallelogram() raises this ValueError when base or height is negative. Both are physical measurements; the library enforces >= 0 before returning base * height. Note this function takes the perpendicular height, not a slanted side, so sign confusion here usually means bad input data rather than a geometry mistake.
Source
Thrown at maths/area.py:387
>>> area_parallelogram(1.6, 2.6)
4.16
>>> area_parallelogram(0, 0)
0
>>> area_parallelogram(-1, -2)
Traceback (most recent call last):
...
ValueError: area_parallelogram() only accepts non-negative values
>>> area_parallelogram(1, -2)
Traceback (most recent call last):
...
ValueError: area_parallelogram() only accepts non-negative values
>>> area_parallelogram(-1, 2)
Traceback (most recent call last):
...
ValueError: area_parallelogram() only accepts non-negative values
"""
if base < 0 or height < 0:
raise ValueError("area_parallelogram() only accepts non-negative values")
return base * height
def area_trapezium(base1: float, base2: float, height: float) -> float:
"""
Calculate the area of a trapezium.
>>> area_trapezium(10, 20, 30)
450.0
>>> area_trapezium(1.6, 2.6, 3.6)
7.5600000000000005
>>> area_trapezium(0, 0, 0)
0.0
>>> area_trapezium(-1, -2, -3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
>>> area_trapezium(-1, 2, 3)
View on GitHub (pinned to f5988cc097)
Solutions
- Validate base >= 0 and height >= 0 before the call; fix the negative source value.
- Normalize signed projections with abs() when only magnitude matters.
- Clean imported data to replace negative sentinels with missing-value markers.
- Catch ValueError for a friendly application-level message.
Example fix
// before area = area_parallelogram(base, height) # height from projection, may be < 0 # after area = area_parallelogram(base, abs(height))
Defensive patterns
Strategy: validation
Validate before calling
if base < 0 or height < 0:
raise ValueError(f'parallelogram base/height must be >= 0: {base}, {height}')
area = area_parallelogram(base, height) Type guard
def is_valid_parallelogram_dims(b: float, h: float) -> bool:
return b >= 0 and h >= 0 Try / catch
try:
area = area_parallelogram(base, height)
except ValueError as e:
raise ValueError(f'invalid parallelogram input: {e}') from e Prevention
- Pass the perpendicular height, not a signed projection.
- Normalize projections with abs() when only magnitude matters.
When it happens
Trigger: Calling area_parallelogram(base, height) with base < 0 or height < 0, e.g. area_parallelogram(-1, 2) or area_parallelogram(1, -2).
Common situations: Land-plot measurement imports with sign conventions; heights computed from coordinate projections that flip sign; -1 sentinels in legacy data files.
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/70decff27ee5481b.
Report an issue: GitHub.