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

3
  • 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 Commented Nov 4, 2014 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. Commented Nov 4, 2014 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 Commented Nov 4, 2014 at 22:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.