我试图创建一个小片的代码,使用毕达哥拉斯定理计算三角形的斜边和高度相对的角的长度。为此,用户必须输入三角形的长度和宽度。我想定义一个函数,以便可以将整个事件称为更大程序的一部分。下面的代码:定义Python函数与用户输入
def ex1() :
from math import sqrt, atan, degrees
print("""Hello, this is a program that will calculate the length of the
hypotenuse of a triangle given the width and height of the triangle.
It will also calculate the angle opposite the height and adjacent to the width.
""")
myWidth = float(input("Please input the width of the triangle: "))
myHeight = float(input("Please input the height of the triangle: "))
hyp = sqrt(((myWidth**2) + (myHeight**2)))
angle = degrees(atan(myHeight/myWidth))
print("\nThe length of the hypotenuse is " + "%.1f" % hyp + " units")
print("\nThe size of the angle opposite the height and \nadjacent to the width is " + "%.1f" % angle + " degrees to 1 decimal place")
input = input("Press enter to end the program\n")
谁能给我解释一下,当我叫它,它抛出,在我这个错误:
UnboundLocalError: local variable 'input' referenced before assignment
提前非常感谢
Python版本您使用的?如果它的'3.x'和上面只有那么''输入'work.Dont分配最后一行到一个变量(即第二个'input'语句。如果你还想要,使用一个不同的变量 – Beginner 2014-11-04 22:34:36
它是,它是Python 3.3.2,所以我不能定义多个用户输入?好的,我会在程序结束时尝试。 – 2014-11-04 22:36:35
总是避免使用python关键字作为变量名。input是一个关键字,它使得它是一个很大的不作为'变量'的名称选择 – Beginner 2014-11-04 22:38:32