2014-12-01 42 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