Reputation: 13
I'm receiving this error and I'm not really sure why considering I've got the import math line there.
NameError: name 'sqrt' is not defined
import math
x = float(input())
y = float(input())
z = float(input())
print('{:.2f} {:.2f} {:.2f} {:.2f}'.format(pow(x, z), pow(x, pow(y, z)), abs(x - y), sqrt(pow(x, z))))
EDIT: I was able to resolve the problem by using math.sqrt
but I'm not sure why it's needed when the pow and abs functions work.
Upvotes: 1
Views: 2608
Reputation: 3987
You have to import sqrt
from math
. Without importing sqrt
you can't use it.
You can try this:
from math import sqrt
Or you can also do:
math.sqrt
pow()
and abs()
are predefined functions in python but sqrt
is not predefined in python. Alternatively, you can use pow(N, 1/2)
as it is equivalent to sqrt(N)
Upvotes: 4
Reputation: 1859
You are currently importing the entire math library and not actually using the sqrt function. You can fix this like so:
import math
x = float(input())
y = float(input())
z = float(input())
print('{:.2f} {:.2f} {:.2f} {:.2f}'.format(pow(x, z), pow(x, pow(y, z)), abs(x - y), math.sqrt(pow(x, z))))
Upvotes: 1