It gave me an error for not recognizing “sqrt”.
I had to add “from math import sqrt” to get it to work.
Anyone else have this issue?
It gave me an error for not recognizing “sqrt”.
I had to add “from math import sqrt” to get it to work.
Anyone else have this issue?
Using just plain math.sqrt is probably a mistake. You’ve already got numpy imported, so why not try np.sqrt? If the input happens to be a scalar, you lose nothing: np.sqrt can handle that as well as arrays.
In general anytime you believe you need to import some additional package, you are making a mistake of some sort and the grader will likely not be happy with that solution. Or perhaps it’s not technically incorrect, as is arguably the case here if the input really is a scalar, but there is already an alternative solution at hand that does not require any additional imports.
Welcome to the community !
I think Paul is quite right. You usually do not need to import additional packages.
Actually, math.sqrt
and np.sqrt
are different.
math.sqrt
is only for “real number”. But, np.sqrt
supports complex number, and “array” as well. In our exercise, majority of variables are “array”. So, it is better to use np.sqrt
for our exercise.
You’re right, thanks for the tip.
I guess I was too blindly following the example, but of course using the NumPy square root function makes the most sense.
If I run into missing imports in the future I will try to find the equivalent in the packages already loaded.
Thanks!