简体   繁体   English

Python:多边形的面积

[英]Python: Area of a polygon

What am I doing wrong, I've been stuck on this for a couple hours.我做错了什么,我已经坚持了几个小时。 I'm trying to compute the area of a polygon and use a loop to print a table of values.我正在尝试计算多边形的面积并使用循环来打印值表。 I am using 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()

There are a number of things going on here, and although your question might be more appropriate for https://codereview.stackexchange.com/ , here's a way to accomplish it:这里有很多事情要做,尽管您的问题可能更适合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()
def report( side_length ):
    length = side_length(get_side_length)
    print('Side Length     Number of Sides     area')

The middle line of this block is strange.这个块的中间线很奇怪。 side_length is a number passed as a parameter, but then you seem to be trying to call it as a function. side_length是作为参数传递的数字,但是您似乎试图将其作为函数调用。 Just delete that line.删除那行就行了。

You never call polygon_area and you never make use of the value it calculates.您从不调用polygon_area ,也从不使用它计算的值。 I guess you want to move that code into report and make it print the result?我猜您想将该代码移动到report并使其打印结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM