我试着用类来计算两点之间的距离
import math
class Point:
#This initializes our class and says that if x,y co-ords are not given then
#the default is x=0, y=0
def __init__(self,x=0,y=0):
self.move(x,y)
#move the point to a new location in 2D space
def move(self,x,y):
self.x=x
self.y=y
#reset the point back to the origin
def reset(self):
self.move(0,0)
#This will find the distance between the 2 points
def CalcDist(self,otherpoint):
return math.sqrt((self.x-otherpoint.x)**2+(self.y-otherpoint.y)**2)
但是,当我试图打印出CalcDist
时,它返回一个错误
我做错什么了?在
CalcDist()
是Point
的成员方法,因此必须使用:M.CalcDist(N)
你应该把最后一行改写为
相关问题 更多 >
编程相关推荐