### Thread: Help with a program that computes the area of a polygon

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
17
Rep Power
0

#### Help with a program that computes the area of a polygon

I thought that this would be simple I was given a formula to use and was given an assignment to make a simple program that computes the area of a polygon. Here is the code I used:

import math
#Input
n = eval(input("Enter the number of sides: "))
s = eval(input("Enter the length of the side: "))

#Calculation
area = area = ((n * s**2) / ((4 * math.tan)*(math.pi/n)))

#Output
print("The area of the polygon is",area)
print('')
print('')

Here is the error I get :
Traceback (most recent call last):
File "/home/barrett/Documents/32-Lab3.py", line 10, in <module>
area = area = ((n * s**2) / ((4 * math.tan)*(math.pi/n)))
TypeError: unsupported operand type(s) for *: 'int' and 'builtin_function_or_method'

Any help with this would be greatly appreciated I am new at this and I look forward to learning and contributing to this forum site as I learn more. Thanks so much!
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
/dev/null
Posts
163
Rep Power
19
Originally Posted by Barretth
#Calculation
area = area = ((n * s**2) / ((4 * math.tan)*(math.pi/n)))
Your formula is wrong. Here's the correct one:

Code:
`area = (n * s**2) / (4 * math.tan((180/n) * (math.pi/180)))`
Remember, argument to math.tan is considered as radians in Python. You've to convert degrees to radians by multiplying it with (PI/180)
3. Originally Posted by barreth
...simple program that computes the area of a polygon.
scared me. I suppose you mean regular polygon.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
19
Rep Power
0
Also, what does this mean:

area = area = ...

AFAIK that will return an error every time unless area is defined in a previous statement.
5. I didn't see that, but it is valid. All the variables get the same definition.
Code:
```>>> a = jungle = out = there = a = jungle = fer = sure = True
>>>
>>> print(a)
True```
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
19
Rep Power
0
so you're saying that this is a legal statement?

Code:
`variable = variable = variable = variable = variable = 1`
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
/dev/null
Posts
163
Rep Power
19
Originally Posted by RickBraun
so you're saying that this is a legal statement?

Code:
`variable = variable = variable = variable = variable = 1`
Yes it is, though it may seem slightly obtuse to type something exactly like that. However, a = b = 1 is perfectly (and sanely) valid.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
17
Rep Power
0

#### I figured it out

import math
#Input
n = eval(input("Enter the number of sides: "))
s = eval(input("Enter the length of the side: "))

#Calculation
area = area = ((n *(s**2)) / (4 * (math.tan(math.pi/n))))

#Output
print("The area of the polygon is",round(area,2))

I thought that it had to do with the parentheses and I was right it was a simple fix I included the code above. Thank you all for your replies though I appreciate it
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
17
Rep Power
0

#### area = area

that was not part of my code I do not know why it keeps showing up like that when I copy and paste the text but at any rate I have figured out the problem.
10. area = area = ...
is kind of silly and will only slow down your program