Second Question for function challenges
Hello everyone, I am on the second challenge for functions and I think I found an error perhaps. The question wants me to use () to find the square root of the argument (num). However from the previous lessons codecademy only has explained how to use () as a power for an argument ex: num2. Please explain to me how you can find the square root of an argument using ().
Write a function named
square_root() that has one parameter named
Use exponents (
** ) to return the square root of
Write your square_root function here:
result = num**;
Uncomment these function calls to test your square_root function:
should print 4
should print 10
Have you learned about functions yet?
The question is asking you to write a function using
def that will calculate the square root of the number it is given.
Here is an example of a function that returns the square of a number:
return n ** 2
You can then call the function:
print(square(5)) #this will print 25
The question is asking me to find the square root not the square.
To find any root of a number, one should raise that number to
1/index of the root.
So in case of a square root, the number should be raised to the power of 0.5 (=1/2).
square root of 9 = 9^0.5 = 3
cubic root of 9 = 9^(1/3) =2,0800838…
root of index 4 of 16 = 9^0.25 = 2
Rational exponents are one way to compute the root, and another is to use the built in
print (math.sqrt(1089)) # 33
squaring a number is the same as doubling its exponent
x ** 1
x ** 2
x ** 4
x ** 8
x ** 16
x ** 32
what happens in the other direction is that the exponent is halved, so that’s how you would do square root.
evaluating it is another story, but
** does that so you don’t need to care about that part.
Why would finding the square root of a number be part of 2nd question of the first challenge? The lessons mention absolutely nothing about finding square roots, only finding the square of a number or using exponents.
return num ** / 2
return / **
return ** /
It all gets a syntax error. I’m guessing it’s because finding a square root means I need a bit more knowledge of Python or coding in general. Which brings me back to my original question.
return num ** / 2 is not valid syntax. Neither are any of the examples you described. Each operator should have the correct amount of operands.
x / y means “divide x by y”.
x ** y means “raise x to the power of y”.
You cannot remove x nor y in any of those two lines I described, for they will become meaningless.
If you are trying to calculate square roots the way I described it, you should write
return num ** (1/2)
return num ** 0.5