2014-12-01 75 views
-1

在這個程序中,我一直收到sidea沒有定義的錯誤,即使我正在返回並調用它。我嘗試過改寫事物的名字,但它仍然不起作用。我該如何解決?謝謝。該錯誤是每=周長(SIDEA,sideb,sidec)行未來 及其NameError:名字「SIDEA」沒有定義變量sidea沒有被定義? Python編程

import math 
import sys 
def main(): 
    x1, y1 = eval(input("\nEnter the coordinates for the points, x1, y1:")) 
    x2, y2 = eval(input("Enter the coordinates for the points, x2, y2:")) 
    x3, y3 = eval(input("Enter the coordinates for the points, x3, y3:")) 

    dist = distance(x1, y1, x2, y2, x3, y3) 
    per = perimeter(sidea, sideb, sidec) 
    are = area(sidea, sideb, sidec, per) 
    vol = volume(area) 

    print("\nThe three points are,", x1, y1, "/", x2, y2, "/", x3, y3) 
    print("\nThe distance between the points is,", "%0.2f" % (dist)) 
    print("\nThe perimter of the triangle is,", "%0.2f" % (per)) 
    print("\nThe area of the triangle is,", "%0.2f" % (are)) 
    print("\nThe volume of the triangle is,", "%0.2f" % (vol)) 

def distance(x1, y1, x2, y2, x3, y3): 
    sidea = (((x2-x1)**2) + ((y2- y1)**2))**(1/2) 
    sideb = (((x3-x2)**2) + ((y3- y2)**2))**(1/2) 
    sidec = (((x3-x1)**2) + ((y3- y1)**2))**(1/2) 
    return sidea, sideb, sidec 
    if ((sidea + sideb) < sidec) and ((sidea + sidec) < sideb) and ((sideb + sidec) < sidea): 
     print("You cannot create a triangle with these points!") 
     (sys.exit()) 

def perimeter(sidea, sideb, sidec): 
    perimeter = sidea + sideb + sidec 
    return perimeter 

def area(sidea, sideb, sidec, perimeter): 
    hp = perimeter/2 
    area = (hp*((hp-sidea)*(hp-sideb)*(hp-sidec)))**(1/2) 
    return area 

def volume(area): 
    h = eval(input("Enter a positive number for the height of the triangle:")) 
    if h > 0: 
     volume = area * h/3 
     return volume 
    else: 
     print("The number entered for the height is not positive!") 
     (sys.exit()) 

main() 
+4

1.請修復您的縮進。 2.在用戶輸入上使用eval是一個糟糕的主意。不,你沒有在main()中定義sidea。 – 2014-12-01 16:03:33

+0

你能否詳細說明我怎麼沒有定義sidea請 – 2014-12-01 16:07:52

+1

那麼,你能指出你認爲你確定的部分嗎? – 2014-12-01 16:08:55

回答

4

您還沒有返回sidea,你已經返回一個元組,並在元組是您sidea變量:

dist = distance(x1, y1, x2, y2, x3, y3) 

但你distance函數返回:

return sidea, sideb, sidec 

調整日第一行是這樣的:

sidea, sideb, sidec = distance(x1, y1, x2, y2, x3, y3) 

這將正確地解壓變量供您使用。

+0

ok thans,但沒有即時獲取typeError:不支持的操作數類型爲*:'function'和'int'line volume = area * h/3 – 2014-12-01 16:17:31

+0

當然你是:'vol = volume(area) 。你在這之前定義了你想用作「are」的變量。相反,您正在將函數'area'傳遞給'volume'。傳遞你的變量,而不是你的函數。 – Andy 2014-12-01 16:20:15

+0

好吧,謝謝 – 2014-12-01 16:26:45