在這個程序中,我一直收到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