-5

Our code lets us input a number, but then does not let us find the square root of it. Is the code only allowing us to square root perfect squares, or is it something else. Here is the code:

elif a == "Square" or a == "Square please":
    print("Please enter a number:")
    num1 = int(input())
    print("Your answer is:")
    answer = sqrt(num1)
    print(answer)

Here is the error: What number would you like squared?: 25

It shows up blank and does not answer.

After adding the math import sqrt right after the if statement, it shows up with this error.

Traceback (most recent call last):
  File "python", line 236, in <module>
  File "python", line 103, in personality
UnboundLocalError: local variable 'sqrt' referenced before assignment

Here is everything I can show you without compromising the confidentiality of the project I am undertaking.

elif a == "Square" or a == "Square please":
    from math import sqrt    
    print("Please enter a number:")
    num1 = int(input())
    print("Your answer is:")
    answer = sqrt(num1)
    print(answer)
Taku
  • 31,927
  • 11
  • 74
  • 85
  • 2
    How you know it does not let you calculate `sqtr()` you get any errors? Please, do post them also in your question – DarkCygnus Jun 19 '17 at 23:23
  • 1
    Try adding `from math import sqrt` to the top of your file. – anonymoose Jun 19 '17 at 23:23
  • 2
    Where is `sqrt` coming from? You can always just find the square root with `num ** 0.5`. – cs95 Jun 19 '17 at 23:23
  • 1
    Did you import `sqrt` from the `math` module? – Luke Jun 19 '17 at 23:24
  • 1
    You say your code "does not let us find the square root". How can you tell? What is it doing instead? If you're getting an exception, please include the full traceback in the question. Otherwise we have no idea what's going on. – Blckknght Jun 19 '17 at 23:25
  • 1
    Possible duplicate of [Python math module](https://stackoverflow.com/questions/8783261/python-math-module) – DarkCygnus Jun 19 '17 at 23:27
  • Hey anata, there's some code you should be showing us. Please paste everything here or else we can't help. – cs95 Jun 19 '17 at 23:30
  • Please provide more code. Your edits aren't very helpful. Also, the import should be done at the very top of the file. – Luke Jun 19 '17 at 23:31

3 Answers3

3

You need to use the sqrt function from the math module. On the top of your file do the following:

from math import sqrt

Alternatively, use the ** operator:

answer = num1 ** 0.5
0

The import should be placed at the start of the file.

from math import sqrt

elif a == "Square" or a == "Square please":
    num1 = int(input("Please enter a number: "))
    print("Your answer is: {}".format(sqrt(num1))

sqrt works with both int and float.

Luke
  • 744
  • 2
  • 7
  • 23
0

Responding to your latest edit:

UnboundLocalError refers to somewhere later in your function, you had defined variable in your function named sqrt. You have to choose a different name for either the sqrt you imported from math by doing so:

from math import sqrt as SquareRoot
# answer = SquareRoot(9)

Or change the other variable inside your function named sqrt (you did not show us that, so I can be sure where that is).

I suspect in your actual code, you did something like this:

def myfunc():
    from math import sqrt    
    print("Please enter a number:")
    num1 = int(input())
    print("Your answer is:")
    answer = sqrt(num1)
    print(answer)
    sqrt = 100 # something else

    # ^ that is what actually is causing the error

Another possibility is that in your actual code, answer variable doesn’t exist, instead you named it sqrt.

Taku
  • 31,927
  • 11
  • 74
  • 85