0

I'm trying to create a small piece of code that uses pythagoras' theorem to calculate the length of the hypotenuse of a triangle and the angle opposite the height. To do this, the user has to enter the length and width of the triangle. I want to define a function so that the whole thing can be called as part of a larger program. Here's the code:

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")

Can anyone explain to me that when I call it, it throws this error at me:

UnboundLocalError: local variable 'input' referenced before assignment

Many Thanks in advance

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Rhys Terry
  • 31
  • 1
  • 8
  • 1
    What version of python are you using? If its `3.x` and above only then will `input` work.Dont assign the last line to a variable( i.e. the second `input` statement. If you still want to, use a different variable – Beginner Nov 04 '14 at 22:34
  • It is, it's Python 3.3.2, so I can't define multiple user inputs? Ok, I'll try that with the end of the program. – Rhys Terry Nov 04 '14 at 22:36
  • Always avoid using python `keywords` as a variable name. `input` is a keyword and that makes it a big no as a `variable` name choice – Beginner Nov 04 '14 at 22:38

2 Answers2

2

See this line here:?

input = ...

See a few lines above where you call the input() function? You've confused the compiler. Use a different name.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

The problem seems to be your last line, where you are assigning a value to the variable 'input'. See this previous SO question: Local Variable Input

Community
  • 1
  • 1
Will
  • 36
  • 3