Python学习记录( 三)

There are several ways to calculate the area of a regular polygon. Given the number of sides, n, and the length of each side, s, the polygon's area is¼ n s2 / tan(π/n).

For example, a regular polygon with 5 sides, each of length 7 inches, has area 84.3033926289 square inches.

Write a function that calculates the area of a regular polygon, given the number of sides and length of each side. Submit the area of a regular polygon with 7 sides each of length 3 inches. Enter a number (and not the units) with at least four digits of precision after the decimal point.

Note that the use of inches as the unit of measurement in these examples is arbitrary. Python only keeps track of the numerical values, not the units.

import math

def area(sides, length):
    
    print ((0.25 * sides) * (length ** 2)) / (math.tan (math.pi / sides))

area(5,7)
area(7,3)


CodeSkulptor链接:http://www.codeskulptor.org/#user38_1C9vSpBjKN_0.py

你可能感兴趣的:(python,polygon)