Python距离类

2024-04-19 20:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用类来计算两点之间的距离

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时,它返回一个错误

^{pr2}$

我做错什么了?在


Tags: thetoimportself距离movedefmath