在对象方向上使用 (x,y) 计算英里 python

calculating miles by using (x,y) in object orientation python

你好,我是面向对象的新手,不知道我做错了什么。问题在于 Taxi 对象拥有的金额,其他一切显然都正常工作。

这是我当前的代码:

class Car()
def __init__( self , mpg=15 , capacity=20 , money=25 ):
#set tank capacity to max and miles is 0
    self.mpg = mpg
    self.fuel = capacity
    self.money = money
    self.capacity = capacity 
    self.x = 0
    self.y = 0
    self.curr_x = 0
    self.curr_y = 0
    self.passenger = False

def driveTo( self , x , y ):
    miles = math.sqrt( ( self.x - x )**2 + ( self.y - y )**2 )
    maxdistance = self.mpg * self.fuel 

    if maxdistance < miles:
        return False
    else: 
        self.x = x
        self.y = y 
        self.fuel -= (miles /  self.mpg) 
        return True

class Taxi(Car):

def pickup(self):
    if self.passenger == False:
        self.passenger = True
        self.curr_x = self.x
        self.curr_y = self.y
        return True  
    else:
        False

def dropoff(self):
    if self.passenger == True:
        dist = (self.x - self.curr_x)**2 + (self.y - self.curr_y)**2 )
        if dist == 0:
            self.money += 2
        else:
            self.money += (2+(3*int(dist)))
        self.passenger = False 
        return True
    else:
        return False 

The Taxi class returns 如果乘客在车内,则上车为 false;如果乘客不在车内,则为下车 returns false。下车后,出租车的接客费用为 2 美元,载客每英里 3 美元。接载乘客时,出租车应记录载客时行驶的英里数。

通过一对坐标进行定位工作 --- y 描述了汽车从原点向北或向南多少英里,x 描述了向东或向西多少英里。

我不知道如何解决这个问题,所以非常感谢您的帮助!如果有任何不清楚的地方,请告诉我,因为我是这个网站的新手:)

这是一道数学题。在 dropoff 方法中计算距离时,您只是忘记了平方根运算。变化

dist = (self.x - self.curr_x)**2 + (self.y - self.curr_y)**2

dist = math.sqrt((self.x - self.curr_x)**2 + (self.y - self.curr_y)**2)

dropoff中,钱看起来会更理智。