TheAlgorithms/Python · error · ValueError
Length must be a positive.
Error message
Length must be a positive.
What it means
Raised by dodecahedron_surface_area() in maths/dodecahedron.py when edge <= 0 or edge is not an int. A regular dodecahedron's surface area 3*sqrt(25+10*sqrt(5))*e^2 only makes sense for a positive edge length, so the function validates it up front. Note the guard is stricter than the message implies: it also rejects floats, so dodecahedron_surface_area(2.5) raises even though 2.5 is a positive length.
Source
Thrown at maths/dodecahedron.py:36
:param edge: length of the edge of the dodecahedron
:type edge: float
:return: the surface area of the dodecahedron as a float
Tests:
>>> dodecahedron_surface_area(5)
516.1432201766901
>>> dodecahedron_surface_area(10)
2064.5728807067603
>>> dodecahedron_surface_area(-1)
Traceback (most recent call last):
...
ValueError: Length must be a positive.
"""
if edge <= 0 or not isinstance(edge, int):
raise ValueError("Length must be a positive.")
return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2)
def dodecahedron_volume(edge: float) -> float:
"""
Calculates the volume of a regular dodecahedron
v = ((15 + (7 * (5** (1 / 2)))) / 4) * (e**3)
where:
v --> is the volume of the dodecahedron
e --> is the length of the edge
reference-->"Dodecahedron" Study.com
<https://study.com/academy/lesson/dodecahedron-volume-surface-area-formulas.html>
:param edge: length of the edge of the dodecahedron
:type edge: float
:return: the volume of the dodecahedron as a float
Tests:View on GitHub (pinned to f5988cc097)
Solutions
- Pass an int edge, or verify whole-number values first: int(edge) if edge == int(edge).
- If you need fractional edge lengths, compute the formula directly with floats: 3 * ((25 + 10 * 5**0.5) ** 0.5) * edge**2.
- Fix upstream units so edge lengths are integral (e.g. work in millimetres instead of metres).
Example fix
# before dodecahedron_surface_area(2.5) # ValueError: Length must be a positive. # after edge = 2.5 area = 3 * ((25 + 10 * 5 ** 0.5) ** 0.5) * edge ** 2 # float formula # or, for whole values: dodecahedron_surface_area(int(edge))
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(edge, int) or isinstance(edge, bool) or edge <= 0:
raise ValueError(f'edge must be a positive int, got {edge!r}') Type guard
def is_positive_int_edge(e) -> bool:
return isinstance(e, int) and not isinstance(e, bool) and e > 0 Try / catch
try:
area = dodecahedron_surface_area(edge)
except ValueError:
area = 3 * ((25 + 10 * 5 ** 0.5) ** 0.5) * float(edge) ** 2 # float fallback formula Prevention
- This API is int-only despite a float-annotated signature — always pass ints.
- Keep a local float formula handy if measurements are fractional.
When it happens
Trigger: Calling dodecahedron_surface_area(-1), dodecahedron_surface_area(0), or dodecahedron_surface_area(5.0) / any float edge. The guard is edge <= 0 or not isinstance(edge, int).
Common situations: Edge lengths computed from geometry that are naturally floats (hypotenuse, scaling factors); CSV/JSON numeric input parsed as float; negative values from a subtraction; bool edges (True passes isinstance int and equals 1).
Related errors
- Monogons and Digons are not polygons in the Euclidean space
- All values must be greater than 0
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
- surface_area_sphere() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/22022085f108cdca.
Report an issue: GitHub.