September 13th, 2013, 12:05 AM

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/32Lab3.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!
September 13th, 2013, 07:10 AM

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)
September 13th, 2013, 07:31 AM

Originally Posted by barreth
...simple program that computes the area of a polygon.
scared me. I suppose you mean regular polygon.
[code]
Code tags[/code] are essential for python code and Makefiles!
September 13th, 2013, 10:41 AM

Also, what does this mean:
area = area = ...
AFAIK that will return an error every time unless area is defined in a previous statement.
September 13th, 2013, 10:59 AM

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
[code]
Code tags[/code] are essential for python code and Makefiles!
September 13th, 2013, 11:02 AM

so you're saying that this is a legal statement?
Code:
variable = variable = variable = variable = variable = 1
September 13th, 2013, 09:22 PM

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.
September 16th, 2013, 12:59 AM

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
September 16th, 2013, 01:02 AM

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.
September 16th, 2013, 11:01 AM

area = area = ...
is kind of silly and will only slow down your program
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25