Ok I'm stuck here. Code is to find the true square root of a number give or take by 0.01. x = 25 epsilon = 0.01 numGuesses = 0 ans = 0.0 while abs(ans**2 - x) >= epsilon and ans <= x: ans += 0.00001 numGuesses += 1 print 'numGuesses =', numGuesses if abs(ans**2 - x) >= epsilon: print 'Failed on square root of', x else: print ans, 'is close to square root of', x -------------------------------------- I can't figure out this line... while abs(ans**2 - x) >= epsilon and ans <= x: ans is 0.0 to begin with, so 0.0squared is 0, minus 25, is -25. Which is
Correct but abs is absolute thus -25 becomes 25 which is indeed >= epsilon and ans which is 0.0 is <= 25
you probably do not need this ' and ans <= x' in your while statement.
I missed that sneaky abs, Thank you!
Hi bwCA, I think maybe that line is there so that we can change the value of epsilon and it will still guarantee that the loops terminates? Because if the step is too big it will skip the answer and keep going?
maybe
Join our real-time social learning platform and learn together with your friends!