1

I am trying to make a function root(x, y) where it finds the yᵗʰ root of x.

I have tried to use math.sqrt() here, but it doesn't work.

Here is my code:

def root(x, y):
    for i in range(y):
        math.sqrt(x)

for i in range(10000):
    print(root(i, 2))

And what it gives me:

None
None
None
None
None

and so on...

Please help me solve this problem.

  • 2
    You do not return anything. You do not store whatever your computation does. I do not see a serious attempt for an [mre] or any sign of debugging attempts... – Patrick Artner Jun 30 '21 at 12:23

2 Answers2

1
def root(x, y):
    return x**(1/y)
FloLie
  • 1,820
  • 1
  • 7
  • 19
1

The nth root of x is just x ** (1.0/n), which you can evaluate directly in Python. Examples:

>>> 27 ** (1.0 / 3)
3.0
>>> 

>>> 32 ** (1.0 / 5)
2.0
>>> 

>>> 10 ** (1.0 / 6)
1.4677992676220695
>>> 

In Python 3, you can use 1 / n in place of 1.0 / n, even if n is an integer, but I use 1.0 just to make it clear that it's a floating point divide.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41