TheAlgorithms/Python · error · ValueError
surface_area_cube() only accepts non-negative values
Error message
surface_area_cube() only accepts non-negative values
What it means
surface_area_cube() raises this ValueError when side_length is negative, because a cube cannot have a negative edge length. The library validates domain constraints before computing 6 * side_length**2 and refuses to silently return a mathematically meaningless result. It is a deliberate input-domain guard, not a bug. Zero and positive floats/ints are accepted.
Source
Thrown at maths/area.py:27
def surface_area_cube(side_length: float) -> float:
"""
Calculate the Surface Area of a Cube.
>>> surface_area_cube(1)
6
>>> surface_area_cube(1.6)
15.360000000000003
>>> surface_area_cube(0)
0
>>> surface_area_cube(3)
54
>>> surface_area_cube(-1)
Traceback (most recent call last):
...
ValueError: surface_area_cube() only accepts non-negative values
"""
if side_length < 0:
raise ValueError("surface_area_cube() only accepts non-negative values")
return 6 * side_length**2
def surface_area_cuboid(length: float, breadth: float, height: float) -> float:
"""
Calculate the Surface Area of a Cuboid.
>>> surface_area_cuboid(1, 2, 3)
22
>>> surface_area_cuboid(0, 0, 0)
0
>>> surface_area_cuboid(1.6, 2.6, 3.6)
38.56
>>> surface_area_cuboid(-1, 2, 3)
Traceback (most recent call last):
...
ValueError: surface_area_cuboid() only accepts non-negative values
>>> surface_area_cuboid(1, -2, 3)
View on GitHub (pinned to f5988cc097)
Solutions
- Inspect the value passed as side_length right before the call and fix the upstream sign error (e.g. swap the operands of the subtraction that produced it).
- If a magnitude is intended, pass abs(side_length) or the correct positive measurement.
- Validate numeric input at ingestion (reject or sanitize negatives) so the guard never fires.
- Wrap the call in try/except ValueError only to convert it into a user-facing message.
Example fix
// before
area = surface_area_cube(edge) # edge == -3 from bad input
# after
if edge < 0:
raise ValueError(f"edge length must be >= 0, got {edge}")
area = surface_area_cube(edge) Defensive patterns
Strategy: validation
Validate before calling
if side_length < 0:
raise ValueError(f'cube side must be >= 0, got {side_length}')
area = surface_area_cube(side_length) Type guard
def is_valid_cube_side(x: object) -> bool:
return isinstance(x, (int, float)) and not isinstance(x, bool) and x >= 0 Try / catch
try:
area = surface_area_cube(side_length)
except ValueError as e:
raise ValueError(f'invalid cube input {side_length!r}: {e}') from e Prevention
- Validate dimensions at data ingestion, not at the math call site.
- Never use negative numbers as missing-value sentinels; use None.
- Compute lengths as abs(delta) when derived from coordinates.
When it happens
Trigger: Calling surface_area_cube(side_length) with side_length < 0, e.g. surface_area_cube(-1). Typically happens when the value comes from a subtraction like (a - b) that can go negative, or from unvalidated user/CSV input.
Common situations: Parsing measurements that use a minus sign as a placeholder for missing data; computing edge length as a difference of coordinates that flips sign; unit-conversion mistakes that negate the value.
Related errors
- 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
- surface_area_conical_frustum() only accepts non-negative val
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7f9118b3b30b363d.
Report an issue: GitHub.