# Define the method distance, inside the class Point, which determines distance between two points.
# Use formula distance = sqrt( (x1-x2)**2 + (y1-y2)**2 + (z1 -z2)**2 ).
# Create two Point objects p2 = Point(4, 5, 6), p3 = Point(-2, -1, 4) and
# determine distance of p2 from p3 using defined distance method.
# Print the distance value.
class Point_1:
def __init__(self,x1,y1,z1):
self.x1=x1
self.y1=y1
self.z1=z1
def __str__(self):
return ('x1'+'='+str(self.x1)+',''y1'+'='+str(self.y1)+',''z1'+'='+str(self.z1))
class Point_2:
def __init__(self,x2,y2,z2):
self.x2=x2
self.y2=y2
self.z2=z2
def __str__(self):
return ('x2'+'='+str(self.x2)+',''y2'+'='+str(self.y2)+',''z2'+'='+str(self.z2))
def distance(p2,p3):
d = math.sqrt(math.pow(x2 - x1, 2) +
math.pow(y2 - y1, 2) +
math.pow(z2 - z1, 2)* 1.0)
print(d)
p2=Point_1(4,5,6)
p3=Point_2(-2,-1,4)
print(p2)
print(p3)
print(distance(p2,p3))
Can somebody tell me what is issue with this code.
I have to pass tow 3 d cordinates and print distance between them.
O/P i am getting is like -
x1=4,y1=5,z1=6
x2=-2,y2=-1,z2=4
NameError Traceback (most recent call last)
in
41 print(p2)
42 print(p3)
—> 43 print(distance(p2,p3))
in distance(p2, p3)
29 def distance(p2,p3):
30
—> 31 d = math.sqrt(math.pow(x2 - x1, 2) +
32 math.pow(y2 - y1, 2) +
33 math.pow(z2 - z1, 2)* 1.0)
NameError: name ‘x2’ is not defined