TheAlgorithms/Python · error · ValueError

Given three sides do not form a triangle

Error message

Given three sides do not form a triangle

What it means

area_triangle_three_sides() raises this ValueError when the three sides violate the triangle inequality: each side must be shorter than the sum of the other two (side1+side2 >= side3 and permutations). Heron's formula requires a realizable triangle; otherwise the value under sqrt() would be negative. Degenerate triangles where one side exactly equals the sum of the others are accepted (area 0).

Solutions

  1. Check the triangle inequality before calling: a+b >= c and a+c >= b and b+c >= a.
  2. Fix the typo/data-entry error in the offending side length.
  3. For near-degenerate cases with floats, compare against a small epsilon tolerance instead of exact inequality.
  4. Catch ValueError and tell the user which side is inconsistent.

Example fix

// before
area = area_triangle_three_sides(a, b, c)

# after
if a + b < c or a + c < b or b + c < a:
    raise ValueError(f'{a}, {b}, {c} do not form a triangle')
area = area_triangle_three_sides(a, b, c)
Defensive patterns

Strategy: validation

Validate before calling

EPS = 1e-12
if side1 + side2 < side3 - EPS or side1 + side3 < side2 - EPS or side2 + side3 < side1 - EPS:
    raise ValueError(f'sides {side1}, {side2}, {side3} do not satisfy the triangle inequality')
area = area_triangle_three_sides(side1, side2, side3)

Type guard

def is_valid_triangle(a: float, b: float, c: float) -> bool:
    return a + b >= c and a + c >= b and b + c >= a

Try / catch

try:
    area = area_triangle_three_sides(a, b, c)
except ValueError as e:
    if 'do not form a triangle' in str(e):
        raise ValueError(f'measurement error: sides {(a, b, c)} are inconsistent') from e
    raise

Prevention

When it happens

Trigger: Calling area_triangle_three_sides(a, b, c) with any side longer than the sum of the other two, e.g. area_triangle_three_sides(2, 7, 4) (2+4 < 7), area_triangle_three_sides(7, 2, 4), or area_triangle_three_sides(1, 2, 3) permutations with strict violation. Note 1,2,3 is degenerate-equal and passes.

Common situations: Random-generated side lengths in tests/fuzzing that mostly don't form triangles; user-entered measurements with typos (e.g. 70 instead of 7); floating-point rounding making a nearly-degenerate triangle strictly violate the inequality.

Related errors


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

Appendix: source

Thrown at maths/area.py:352

        ...
    ValueError: area_triangle_three_sides() only accepts non-negative values
    >>> area_triangle_three_sides(2, 4, 7)
    Traceback (most recent call last):
        ...
    ValueError: Given three sides do not form a triangle
    >>> area_triangle_three_sides(2, 7, 4)
    Traceback (most recent call last):
        ...
    ValueError: Given three sides do not form a triangle
    >>> area_triangle_three_sides(7, 2, 4)
    Traceback (most recent call last):
        ...
    ValueError: Given three sides do not form a triangle
    """
    if side1 < 0 or side2 < 0 or side3 < 0:
        raise ValueError("area_triangle_three_sides() only accepts non-negative values")
    elif side1 + side2 < side3 or side1 + side3 < side2 or side2 + side3 < side1:
        raise ValueError("Given three sides do not form a triangle")
    semi_perimeter = (side1 + side2 + side3) / 2
    area = sqrt(
        semi_perimeter
        * (semi_perimeter - side1)
        * (semi_perimeter - side2)
        * (semi_perimeter - side3)
    )
    return area


def area_parallelogram(base: float, height: float) -> float:
    """
    Calculate the area of a parallelogram.

    >>> area_parallelogram(10, 20)
    200
    >>> area_parallelogram(1.6, 2.6)
    4.16

View on GitHub (pinned to f5988cc097)