{"record":{"id":"9bcb003840ce018a","repo":"TheAlgorithms/Python","slug":"area-reg-polygon-only-accepts-integers-greater-t","errorCode":null,"errorMessage":"area_reg_polygon() only accepts integers greater than or equal to three as number of sides","messagePattern":"area_reg_polygon\\(\\) only accepts integers greater than or equal to three as number of sides","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":545,"sourceCode":"three as number of sides\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    >>> 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","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L527-L563","documentation":"area_reg_polygon() raises this ValueError when sides is not an int or is less than 3. A regular polygon needs at least 3 sides (triangle); the library also rejects floats like 4.0 and bools count only via isinstance quirks, because the formula (n * s^2) / (4 * tan(pi/n)) assumes an integer side count. This is a type-and-domain check, distinct from the separate negative-length check on the second parameter.","triggerScenarios":"Calling area_reg_polygon(sides, length) with sides as a non-int (e.g. 4.5 or '6') or an int < 3 (e.g. -1, 0, 1, 2), e.g. area_reg_polygon(-1, 2) or area_reg_polygon(4.0, 2). Passing a numpy integer can also fail since isinstance(np.int64(4), int) is False on most platforms.","commonSituations":"User input parsed as float ('4.0' from a form) and passed straight in; numpy/pandas integer types flowing into the function; loop variables starting at 0 or 1; JSON configs where the side count deserializes as float.","solutions":["Coerce to int explicitly when the value is integral: sides = int(round(sides)).","Validate sides >= 3 before the call and reject bad config early.","Convert numpy integers with int(sides) before calling.","Catch ValueError and report the offending sides value."],"exampleFix":"// before\narea = area_reg_polygon(user_input, s)  # user_input is 4.0 (float from JSON)\n\n# after\nif not isinstance(sides, int) or sides < 3:\n    sides = int(round(float(user_input)))\narea = area_reg_polygon(sides, s)","handlingStrategy":"type-guard","validationCode":"if not isinstance(sides, int) or isinstance(sides, bool) or sides < 3:\n    raise ValueError(f'sides must be an int >= 3, got {sides!r}')\narea = area_reg_polygon(sides, length)","typeGuard":"def is_valid_polygon_sides(n: object) -> bool:\n    \"\"\"Type guard: n is a true int (not bool) with value >= 3.\"\"\"\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 3","tryCatchPattern":"try:\n    area = area_reg_polygon(sides, length)\nexcept ValueError as e:\n    raise ValueError(f'invalid polygon params (sides={sides!r}, length={length!r}): {e}') from e","preventionTips":["Coerce float side counts with int(round(x)) only after confirming x.is_integer().","Convert numpy/pandas integers to int() before calling.","Validate config values (sides >= 3, length >= 0) at load time."],"tags":["python","math","geometry","validation","valueerror","type-check","polygon"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}