Python:多边形的面积

时间:2015-11-19 02:08:18

标签: python

我做错了什么,我被困在这几个小时。我正在尝试计算多边形的面积并使用循环来打印值表。我正在使用python

# This program computes 
# the area of polygons
import math

def main():
    side_length = get_side_length()
    report (side_length)

def report( side_length ):
    length = side_length(get_side_length)
    print('Side Length     Number of Sides     area')

def polygon_area( num_sides, side_length ):
    for num_sides in range(3, 10):
        area = (num_sides * side_length * side_length) \
        / (4 * math.tan(math.pi / num_sides)) 


def get_side_length():
    side_length = int(input( 'Input the length of a side '))
    return side_length


# start the program
main()

2 个答案:

答案 0 :(得分:1)

此处有很多内容,虽然您的问题可能更适合https://codereview.stackexchange.com/,但这是实现目标的方法:

# This program computes 
# the area of polygons
import math

def main():
    side_length = get_side_length()
    for number_of_sides in range(3, 10):
        report (side_length, number_of_sides)

def report(side_length, number_of_sides):
    area = polygon_area(number_of_sides, side_length)
    print('Side Length: {0}    Number of Sides: {1} Area: {2}'.format(side_length, number_of_sides, area))

def polygon_area( num_sides, side_length ):
    area = (num_sides * side_length * side_length) / (4 * math.tan(math.pi / num_sides)) 

def get_side_length():
    return int(input( 'Input the length of a side '))

# start the program
main()

答案 1 :(得分:0)

def report( side_length ):
    length = side_length(get_side_length)
    print('Side Length     Number of Sides     area')

这个街区的中间线很奇怪。 side_length是作为参数传递的数字,但是您似乎试图将其称为函数。只需删除该行。

您永远不会致电polygon_area而您永远不会使用它计算的值。我想您想将该代码移到report并打印结果?