The code editor I’m using doesn’t let me use use the sqrt() function. I’ve tried math.sqrt() as well. What should I do? Is there a way you can make a square root function?
import math to the beginning of your code, then use
If you don’t like typing math.sqrt(), and you’ll need it repeatedly, you could do this:
import math def sr(x): return math.sqrt(x) print (sr(16)) #Output: 4
Without using the
math module, we can use rational exponents to compute the
def nth_root(x, n): return x ** (1 / n)
That will work as expected in Python 3. In Python 2 we need to make the numerator or the denominator into a float.
def nth_root(x, n): return x ** (1.0 / n)
def sq_rt(x): return x**0.5