0

I am new to python and am trying to make a calculator. I have +, -, *, / and raising to the power of x done and am trying to do have a function that finds the square root of a number. I had a version working but it only worked if there was a number that was exactly the square root. As in 3 is exactly the square root of 9 but if the square root had a decimal such as the square root of 6.5 it would crash. I wanted it to be able to do the float numbers as well. The way I was doing it originally was:

tempAns = 1
while tempAns <= num1:
    if tempAns * tempAns == num1:
        answer = tempAns
        break

    tempAns += 1

print('The answer is: ' + str(answer))

But my problem was that as I was incrementing it by 1 it was missing the numbers in between. I tried to increment it by 0.1 each time with:

tempAns = 0.1
while tempAns <= num1:
    if tempAns * tempAns == num1:
        answer = tempAns
        break

    tempAns += 0.1

print('The answer is: ' + str(answer))

But this just gave me an error saying: UnboundLocalError: local variable 'answer' referenced before assignment

I have absolutely no idea why this is happening as it should be looping until it assigns a number to 'answer'.

Any help will be appreciated! Also any suggestions on how to make my code cleaner would be nice as I'm new to python. Thanks in advance!

5
  • Why aren't you using math.sqrt()? Commented Nov 14, 2022 at 20:08
  • 1
    You'll get that error if the while condition is false at the beginning, so the loop never runs. You should give answer a default value before the loop. Commented Nov 14, 2022 at 20:09
  • 1
    You want to use a numerical root-finding method, rather than just linearly searching for a number that happens to be the correct one. A simple root-finding method that should work "okay" for this problem is Newton's method. Also you want to exit when the result is "close enough" rather than exactly right, otherwise you will find that due to floating point rounding, you sometimes won't find any answer for a given input. Commented Nov 14, 2022 at 20:10
  • 1
    For example, sqrt(3) is approximately 1.7320508075688772. If you were to search through every decimal number with 17 decimal places, starting at 0, it would take you a very long time to reach this value and confirm that when you square it you get 3. Furthermore, when you square this number, you actually get 2.9999999999999996, not 3, so your method would fail to find this result. Commented Nov 14, 2022 at 20:15
  • 1
    What Every Computer Scientist Should Know About Floating-Point Arithmetic Commented Nov 14, 2022 at 20:40

1 Answer 1

1

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number.

Syntax: math.sqrt(x)

Parameter: x is any number such that x>=0

Returns: It returns the square root of the number passed in the parameter.

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

1 Comment

Yep, thanks for this, I wasn't using this in my code for two reasons, I hadn't realised it was a thing and also I like making the functions my self just to give myself things to do. Thanks though!

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.