{"record":{"id":"af2077e0b658d24d","repo":"TheAlgorithms/Python","slug":"area-reg-polygon-only-accepts-non-negative-value","errorCode":null,"errorMessage":"area_reg_polygon() only accepts non-negative values as length of a side","messagePattern":"area_reg_polygon\\(\\) only accepts non-negative values as length of a side","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":550,"sourceCode":"three as number of sides\r\n    >>> area_reg_polygon(5, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_reg_polygon() only accepts non-negative values as \\\r\nlength of a side\r\n    >>> area_reg_polygon(-1, 2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_reg_polygon() only accepts integers greater than or equal to \\\r\nthree as number of sides\r\n    \"\"\"\r\n    if not isinstance(sides, int) or sides < 3:\r\n        raise ValueError(\r\n            \"area_reg_polygon() only accepts integers greater than or \\\r\nequal to three as number of sides\"\r\n        )\r\n    elif length < 0:\r\n        raise ValueError(\r\n            \"area_reg_polygon() only accepts non-negative values as \\\r\nlength of a side\"\r\n        )\r\n    return (sides * length**2) / (4 * tan(pi / sides))\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod(verbose=True)  # verbose so we can see methods missing tests\r\n\r\n    print(\"[DEMO] Areas of various geometric shapes: \\n\")\r\n    print(f\"Rectangle: {area_rectangle(10, 20) = }\")\r\n    print(f\"Square: {area_square(10) = }\")\r\n    print(f\"Triangle: {area_triangle(10, 10) = }\")\r\n    print(f\"Triangle: {area_triangle_three_sides(5, 12, 13) = }\")\r\n    print(f\"Parallelogram: {area_parallelogram(10, 20) = }\")\r\n    print(f\"Rhombus: {area_rhombus(10, 20) = }\")\r","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L532-L568","documentation":"Raised by area_reg_polygon() when the side length passed to it is negative. The function computes the area of a regular polygon from its number of sides and side length; a negative length is geometrically meaningless, so it refuses it. Note the sides check runs first, so a bad sides value masks this error.","triggerScenarios":"area_reg_polygon(6, -3) — any call where length < 0 while sides is an int >= 3, e.g. area_reg_polygon(5, -1.2).","commonSituations":"Feeding user input or measurements into the function without sanitizing sign; sign flips from upstream subtraction (e.g. length = a - b where b > a); CSV/parsed data containing negative values.","solutions":["Validate/abs or reject negative lengths before calling area_reg_polygon.","Check the provenance of the length value — a negative usually indicates an upstream calculation bug.","If zero-length sides are acceptable for your use, confirm they return 0 area rather than erroring (they do — only < 0 raises)."],"exampleFix":"// before\narea = area_reg_polygon(sides, length)  # length may be -3\n\n# after\nif length < 0:\n    raise ValueError(f\"side length must be >= 0, got {length}\")\narea = area_reg_polygon(sides, length)","handlingStrategy":"validation","validationCode":"if not isinstance(sides, int) or sides < 3:\n    raise ValueError(\"sides must be an int >= 3\")\nif length < 0:\n    raise ValueError(f\"length must be non-negative, got {length}\")","typeGuard":"def is_valid_polygon_input(sides: object, length: object) -> bool:\n    return isinstance(sides, int) and sides >= 3 and isinstance(length, (int, float)) and length >= 0","tryCatchPattern":null,"preventionTips":["Validate numeric inputs at the parse boundary before math calls","Remember sides is validated before length — fix sides errors first in doctests"],"tags":["math","geometry","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}