random_user_0891
random_user_0891

Reputation: 2051

Python: area of a regular polygon

I'm trying to calculate the area of a "regular polygon" (a regular polygon means that all sides of the polygon are the same). I created a method to do it, however, it seems to be off by "1" and I can't seem to figure out why.

import math

if __name__=="__main__":

    num_sides = int(input("Enter the number of sides: "))
    side_length = float(input("Enter the side: "))

    def polygon_area(n_sides, length):
        area = (n_sides * (length ** 2)) / (4 * math.tan((math.pi) / n_sides))
        print(area)

    polygon_area(num_sides, side_length)

here is the formula I'm using to find the area of a regular polygon given 1 side enter image description here

here is the expected output that i am supposed to get

enter image description here

So the expected result is supposed to be 73.69017017488385, but I get 72.69017017488385. I thought it could be the order of operations or how the user input was being handled but they seem ok. I'm not sure at this point why it's off by 1.

Upvotes: 1

Views: 2503

Answers (1)

DYZ
DYZ

Reputation: 57033

This is a well-known error in Liang's book. The right answer is 72.69017017488385. As a side note, do not print() values in a function. return them and let the caller do the printing.

Upvotes: 5

Related Questions