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?
1 Like
Add import math
to the beginning of your code, then use math.sqrt()
.
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
3 Likes
Without using the math
module, we can use rational exponents to compute the nth
root.
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)
3 Likes
def sq_rt(x):
return x**0.5
2 Likes