在这个程序中,我一直收到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()
1.请修复您的缩进。 2.在用户输入上使用eval是一个糟糕的主意。不,你没有在main()中定义sidea。 – 2014-12-01 16:03:33
你能否详细说明我怎么没有定义sidea请 – 2014-12-01 16:07:52
那么,你能指出你认为你确定的部分吗? – 2014-12-01 16:08:55