Area of a polygon

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

Area of a polygon

Postby nzieno » Wed Sep 14, 2016 11:31 pm

I need to find the area of a regular polygon. the program enters the amount of sides and the length of the sides and outputs the area. my problems comes when the program is supposed to output the area I get this error File "/Users/nickzieno/Polygon_area.py", line 7, in <module>
area = (n * s ** 2) / (4 * math.tan((180 / n) * (math.pi / 180)))
builtins.TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

Code: Select all
#this program will find the area of a polygon.

import math

s = input("Enter the number of sides:")
n = input("Enter the length of the side:")
area = (n * s ** 2) / (4 * math.tan((180 / n) * (math.pi / 180)))

print("The area of the polygon is:", area)
nzieno
 
Posts: 16
Joined: Fri Jul 29, 2016 12:38 am

Re: Area of a polygon

Postby micseydel » Wed Sep 14, 2016 11:47 pm

It looks like you're using Python 3, where input() returns a string. You can't exponentiate a string. What you want instead is for
Code: Select all
input("Enter the number of sides:"

to be
Code: Select all
int(input("Enter the number of sides:")) 

This way, you tell Python to apply the function int() to the string returned by input(), and then s will contain the result of int().
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Area of a polygon

Postby nzieno » Fri Sep 16, 2016 12:25 pm

Ok so I have that part working but I had to change it to eval so I could use decimals. But I don't believe my formula is correct because my outcome is not what the rest of the class is getting for the exercise problem in the book. the answer I get is
Enter the number of sides:5
Enter the length of the side:6.5
The area of the polygon is: 77.4044704591872 The rest of the class is getting 72.69017017488386


Code: Select all
#this program will find the area of a polygon.

import math

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


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

print("The area of the polygon is:", area)
nzieno
 
Posts: 16
Joined: Fri Jul 29, 2016 12:38 am

Re: Area of a polygon

Postby Ofnuts » Fri Sep 16, 2016 12:47 pm

I'll bet that if you enter "5." instead of "5" you'll get the right result, because your eval() will return a float instead of an int.

Dirty fix: Replace your uses of eval() by float() to make sure you deal with float values

Clean fix: Replace eval() by int() for the sides, and eval() by float() for the length. And then replace math.tan((180 / n) * (math.pi / 180)) by the completely mathematically equivalent math.tan(math.pi / n) where there is no hidden integer division.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: Area of a polygon

Postby nzieno » Fri Sep 16, 2016 1:54 pm

Got it now thanks for the help I also had to switch my variables S and N with each other and I got the answer I was looking for. :D
nzieno
 
Posts: 16
Joined: Fri Jul 29, 2016 12:38 am


Return to Homework

Who is online

Users browsing this forum: No registered users and 4 guests