Tadaa! This is my first attempt at writing in Python, trying to solve a good question posed here

``````#-----------------------------------------------------------------------------------------------
#
# Python 2.7
#
# Calculating area and radius of polygons with known number of sides and the length of this side
# Adapted formulas used from this site: http://www.mathwords.com/a/area_regular_polygon.htm
# DM 17/09/2013 first attempt to produce a plusminus decent Python script
#-----------------------------------------------------------------------------------------------

import math
from string import Template

def Calculate_PolygonArea (NumberofSides, SideLength ):
return ( NumberofSides * SideLength ** 2 ) / (4 * math.tan( math.pi / NumberofSides ))

return SideLength / (2 * math.sin( math.pi / NumberofSides ))

NSides = 100000
SLength = 0.0001

Area = Calculate_PolygonArea( NSides, SLength )

StrTmpl = Template('For a polygon with number of sides \${sides} and side length of \${len}')
print ( StrTmpl.safe_substitute( sides=NSides, len=SLength ))
print('Radius is equal to 5.0 / math.pi : ', 5.0 / math.pi )

dummy = input()
``````

And I don't seem to find an answer why the limit seems to diverge to 5/pi.
Anyone to help me out?

Congratulations, this is good python code. Its naming style however is unusual for python code. You may want to read pep 8, a style guide written for the developpers of the python language trunk. If you follow this …

My formatting of your calculations (notice that `len` is builtin function do not use it as variable name):

``````""" calc_polyarea.py
Calculating area and radius of polygons with known number of sides and the length of this side
Adapted formulas used from this site: http://www.mathwords.com/a/area_regular_polygon.htm
DM 17/09/2013 (with little help from …``````

Very good!

Follow the Python style guide and your code will look rather polished.

I had to smile when I saw the long-worded camel style of C# show up in your Python code. My problem is that I am not that good of a typist.

The radius was a little …

## All 7 Replies

Congratulations, this is good python code. Its naming style however is unusual for python code. You may want to read pep 8, a style guide written for the developpers of the python language trunk. If you follow this style everybody will think you are a very experienced pythonista.

Also `string.Template` is seldom used. Most programmers would choose the method `str.format()` for this.

The 5/pi limit explains easily. The polygon perimeter is 10 in your example `(n * s)`. This is the perimeter of a circle with radius 5/pi.

My formatting of your calculations (notice that `len` is builtin function do not use it as variable name):

``````""" calc_polyarea.py
Calculating area and radius of polygons with known number of sides and the length of this side
Adapted formulas used from this site: http://www.mathwords.com/a/area_regular_polygon.htm
DM 17/09/2013 (with little help from friends) first attempt to produce a plusminus decent Python script
for Python 2.7

"""
from __future__ import print_function
import math

def polygon_area (number_of_sides, side_length):
return (number_of_sides * side_length ** 2) / (4 * math.tan(math.pi / number_of_sides))

return side_length / (2 * math.sin(math.pi / number_of_sides))

sides = 100000
length = 0.0001

area = polygon_area(sides, length)

print('For a polygon with number of sides {sides} and side length of {length}'.format(sides=sides, length=length))
print('Radius is equal to 5.0 / math.pi : ', 5.0 / math.pi)

"""Output:
For a polygon with number of sides 100000 and side length of 0.0001
Area =  7.95774715 and Radius = 1.59154943
Radius is equal to 5.0 / math.pi :  1.59154943092
"""
``````

Very good!

Follow the Python style guide and your code will look rather polished.

I had to smile when I saw the long-worded camel style of C# show up in your Python code. My problem is that I am not that good of a typist.

The radius was a little trick question, it assumed the perimeter of a polygon being `s * n` and that with high n and short s being almost equal to a circle's circumference from which you could have calculated r

Thanks to all of you for the help.

My problem is that I am not that good of a typist.

Did you try a dvorak keyboard vegaseat ? I learnt the bépo keyboard for french and I'm a much better typist now...

Don't worry Vegaseat. I just can't type either. It just so happens I do like the somewhat verbose style often used in C#. And I never use one letter variables. Only exceptions: i and j as counters in a loop or x and y as parameters for a function.
My top typing work consisted of a system that went down and only understood input from a QWERTY keyboard. In our country we use AZERTY keyboards and I managed to bring it up again typing some Unix commands... Sometimes I still have nightmares about it!

I am using my Toshiba Satellite Notebook most of the time since it has an extra large screen (helps my poor eye sight) and a full qwerty keyboard.

If I remember correctly, somebody developed the BOO language about 10 years ago to get away from typing all those long C# words. If you are curious, I tasted it here:
http://www.daniweb.com/software-development/python/code/216668/a-taste-of-boo

A better compromise for the C# coder is IronPython, at least your GUI code will look familiar. The nice thing about IronPython is that you can compile the code to a nice dense executable file, at least on Windows boxes (don't know much about MONO). See a sample code at: