{"record":{"id":"fb70f562a0a56b1d","repo":"TheAlgorithms/Python","slug":"given-three-sides-do-not-form-a-triangle","errorCode":null,"errorMessage":"Given three sides do not form a triangle","messagePattern":"Given three sides do not form a triangle","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":352,"sourceCode":"        ...\r\n    ValueError: area_triangle_three_sides() only accepts non-negative values\r\n    >>> area_triangle_three_sides(2, 4, 7)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Given three sides do not form a triangle\r\n    >>> area_triangle_three_sides(2, 7, 4)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Given three sides do not form a triangle\r\n    >>> area_triangle_three_sides(7, 2, 4)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Given three sides do not form a triangle\r\n    \"\"\"\r\n    if side1 < 0 or side2 < 0 or side3 < 0:\r\n        raise ValueError(\"area_triangle_three_sides() only accepts non-negative values\")\r\n    elif side1 + side2 < side3 or side1 + side3 < side2 or side2 + side3 < side1:\r\n        raise ValueError(\"Given three sides do not form a triangle\")\r\n    semi_perimeter = (side1 + side2 + side3) / 2\r\n    area = sqrt(\r\n        semi_perimeter\r\n        * (semi_perimeter - side1)\r\n        * (semi_perimeter - side2)\r\n        * (semi_perimeter - side3)\r\n    )\r\n    return area\r\n\r\n\r\ndef area_parallelogram(base: float, height: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a parallelogram.\r\n\r\n    >>> area_parallelogram(10, 20)\r\n    200\r\n    >>> area_parallelogram(1.6, 2.6)\r\n    4.16\r","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L334-L370","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the triangle inequality before calling: a+b >= c and a+c >= b and b+c >= a.","Fix the typo/data-entry error in the offending side length.","For near-degenerate cases with floats, compare against a small epsilon tolerance instead of exact inequality.","Catch ValueError and tell the user which side is inconsistent."],"exampleFix":"// before\narea = area_triangle_three_sides(a, b, c)\n\n# after\nif a + b < c or a + c < b or b + c < a:\n    raise ValueError(f'{a}, {b}, {c} do not form a triangle')\narea = area_triangle_three_sides(a, b, c)","handlingStrategy":"validation","validationCode":"EPS = 1e-12\nif side1 + side2 < side3 - EPS or side1 + side3 < side2 - EPS or side2 + side3 < side1 - EPS:\n    raise ValueError(f'sides {side1}, {side2}, {side3} do not satisfy the triangle inequality')\narea = area_triangle_three_sides(side1, side2, side3)","typeGuard":"def is_valid_triangle(a: float, b: float, c: float) -> bool:\n    return a + b >= c and a + c >= b and b + c >= a","tryCatchPattern":"try:\n    area = area_triangle_three_sides(a, b, c)\nexcept ValueError as e:\n    if 'do not form a triangle' in str(e):\n        raise ValueError(f'measurement error: sides {(a, b, c)} are inconsistent') from e\n    raise","preventionTips":["Validate the triangle inequality yourself with an epsilon for float data.","Use triangle-valid generation (e.g. sample two angles) when fuzzing.","Treat this error as a strong typo signal in user-entered side lengths."],"tags":["python","math","geometry","validation","valueerror","triangle-inequality","herons-formula"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}