我是一名初学者,使用Python3.6,我不知道为什么我的代码无法正常工作。非常感谢您的反馈/帮助。是“ZeroDivisionError:除零”
我对DriveTo功能说明如下:
现在,添加一个汽车方法driveTo。它应该有两个额外的参数,即汽车试图移动的位置的x和y坐标。如果汽车有足够的气体进行行程,汽车应该移动,剩余的汽油量应该更新,方法应该返回True。如果汽车没有足够的气体,它不应该被移动或根本改变,该方法应返回False
这里是我当前的代码:
import math
class Car:
def __init__(self , mpg , fuel , money):
self.mpg = mpg
self.fuel = fuel
self.money = money
#return current location of car in two element list
def getLocation(self):
return [ self.x , self.y ]
#returns the number of gallons left in the car
def getGas(self):
self.fuel -= 1
return self.fuel
#returns how much gas the car needs to be at capacity
def getToFill(self):
current_tank = self.getGas()
gas_needed = (self.fuel - current_tank)
return gas_needed
#return true or false if enough gas
def driveTo(self , x , y):
self.x = x
self.y = y
miles_pg = (self.mpg/self.getToFill())
miles = math.sqrt((self.x - x)**2 + (self.y - y)**2)
if miles >= miles_pg:
return True
else:
return False
您可以添加如何测试代码 – Vallentin
您可以指定哪条线发生错误? –
将整个堆栈跟踪放入问题中。它会向您(和我们)显示您的错误发生的确切位置,然后可以向后追溯这些值以查看错误发生的位置。 – skrrgwasme