我是一名初學者,使用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