TheAlgorithms/Python · error · ValueError
Monogons and Digons are not polygons in the Euclidean space
Error message
Monogons and Digons are not polygons in the Euclidean space
What it means
Raised by check_polygon() in maths/check_polygon.py when the input list of side lengths has fewer than 2 entries. The function decides whether side lengths can form a polygon; with 0 or 1 sides there is no polygon in Euclidean space (a monogon/digon is degenerate), so it raises ValueError before checking the triangle-inequality-style condition.
Source
Thrown at maths/check_polygon.py:33
>>> check_polygon([3, 7, 13, 2])
False
>>> 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
- Ensure the input list contains at least 2 side lengths (in practice at least 3 for a real polygon) before calling.
- Fix the upstream collection step that produced an empty or single-element list of side lengths.
- Validate parsed input (e.g. after split/strip of a user string) before passing to check_polygon.
Example fix
# before
sides = []
check_polygon(sides) # ValueError
# after
sides = [3, 7, 13, 2]
if len(sides) < 3:
raise SystemExit('need at least 3 side lengths')
check_polygon(sides) Defensive patterns
Strategy: validation
Validate before calling
if len(sides) < 3:
raise ValueError(f'need at least 3 sides, got {len(sides)}') Type guard
def is_polygon_input(nums) -> bool:
return isinstance(nums, (list, tuple)) and len(nums) >= 3 Try / catch
try:
check_polygon(sides)
except ValueError as e:
if 'Monogons' in str(e):
sides = collect_more_sides() # re-prompt / re-parse
else:
raise Prevention
- Treat an empty or tiny side list as invalid input at parse time, not inside the math call.
- Distinguish 'not enough sides' from 'bad side values' in your own error messages.
When it happens
Trigger: Calling check_polygon([]) (empty list) or check_polygon([5]) (single side). The guard is len(nums) < 2.
Common situations: Feeding an empty dataset or a filtered list that ended up empty into a geometry pipeline; parsing side lengths from user input or a file where only one value was extracted; forgetting that a triangle needs 3 sides and testing with fewer.
Related errors
- All values must be greater than 0
- Length must be a positive.
- 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/e07f6f82b2763fd5.
Report an issue: GitHub.