使用Python面向对象计算(x,y)中的英里数

2024-04-23 10:55:39 发布

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

你好,我是一个全新的面向对象,不能找出我在这里做错了什么。问题在于出租车对象拥有的资金量,其他一切显然都正常工作。你知道吗

这是我当前的代码:

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 

如果乘客在车内,出租车类会返回false,如果乘客不在车内,出租车类会返回false。下车后,计程车收取2美元的接送费和3美元每英里驾驶的乘客。当乘客被接走时,出租车应该在载客时追踪其行驶的里程。你知道吗

通过一对坐标进行定位——y表示汽车从原点向北或向南行驶了多少英里,x表示汽车向东或向西行驶了多少英里。你知道吗

我不知道如何解决这个问题,所以非常感谢您的帮助!让我知道如果有什么是不清楚的,因为我是新到这个网站:)


Tags: selffalsetruereturnifdefelsecapacity
1条回答
网友
1楼 · 发布于 2024-04-23 10:55:39

这是一个数学问题。在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和金钱将看起来更理智。你知道吗

相关问题 更多 >