#1
  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. #2
  3. 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)
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    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!
  6. #4
  7. 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.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    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!
  10. #6
  11. 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
  12. #7
  13. 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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    17
    Rep Power
    0

    Smile 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
  16. #9
  17. 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.
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    608
    Rep Power
    65
    area = area = ...
    is kind of silly and will only slow down your program
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo