2015-11-19 142 views
-1

我在做什麼錯,我一直堅持了幾個小時。我正在嘗試計算多邊形的面積並使用循環來打印值表。我使用pythonPython:多邊形的面積

# 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() 
+0

你期望發生的?出了什麼問題? – poolie

+0

我想要一個循環說如果我有一個4行的長度是什麼區域將是如果行數是3至9.什麼即時通訊問題正在實際工作 – UchiaSky

+0

你需要輸入邊的數量,作爲固定值或從用戶 – kponz

回答

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() 
0
def report(side_length): 
    length = side_length(get_side_length) 
    print('Side Length  Number of Sides  area') 

該塊的中間線是奇怪。 side_length是一個作爲參數傳遞的數字,但是您似乎試圖將其作爲函數調用。只需刪除該行。

你永遠不會打電話polygon_area,你永遠不會使用它計算的值。我想你想將該代碼移動到report並使其打印結果?