我正在定义一个叫做“汽车”的课程。我将它与一个在我班上运行一系列测试的文档进行比较。但是,我在Drive Method中得到了错误的答案,我不知道为什么。
这是我的代码。 Drive方法应该将汽车移动并将其移动指定的里程数作为参数。如果汽车可以在没有燃料耗尽的情况下达到所有里程,那么汽车就行驶并输出里程,同时增加里程表。如果它不能进行旅行并且仍然有油箱中的燃油,那么它会驱动很长时间,直到fuelLevel
为0。
如果给出了任何无效参数,那么汽车不应该开车。每次课程进入测试9'在Car Tester计划中,它打破了。因此所有后续的都会破裂。下面我先发布了我的代码,然后是代码来测试它。
你能帮帮我吗?我很感激#Define the class
class Car(object):
def __init__(self,fuelEfficiency=0,fuelCapacity=0,fuelLevel=0,odometer=0):
self.setCar(fuelEfficiency,fuelCapacity,fuelLevel,odometer)
def setFuelEfficiency(self,newFuelEfficiency):
self.setCar(fuelEfficiency = newFuelEfficiency)
def setFuelCapacity(self,newFuelCapacity):
self.setCar(fuelCapactity = newFuelCapacity)
def setFuelLevel(self,newFuelLevel):
self.setCar(fuelLevel = newFuelLevel)
def setOdometer(self,newOdometer):
self.setCar(odometer = newOdometer)
def setCar(self,fuelEfficiency = None,fuelCapacity = None,fuelLevel = None,odometer = None):
if fuelEfficiency == None:
fuelEfficiency = self.getFuelEfficiency
if fuelCapacity == None:
fuelCapacity = self.getFuelCapacity
if fuelLevel == None:
fuelLevel = self.getFuelLevel
if odometer == None:
odometer = self.getOdometer
self.fuelEfficiency = fuelEfficiency
self.fuelCapacity = fuelCapacity
self.fuelLevel = fuelLevel
self.odometer = odometer
def drive(self,miles):
if miles < 0:
return ("The car is not driven")
one_gallon = miles / self.fuelEfficiency
if one_gallon < self.fuelLevel:
print("The car drove {} miles".format(miles))
elif self.fuelLevel == 0:
print("The car drove 0 miles")
#else:
#newMiles = milesDriven * miles
#print("The car drove {} miles".format(newMiles))
self.fuelLevel -= one_gallon
self.odometer += miles
def getCar(self):
#Returns a tuple that has (FE,FC,FL,OD)
return (self.fuelEfficiency,self.fuelCapacity,self.fuelLevel,self.odometer)
def addFuel(self,num):
if type(num) == str:
return self.fuelLevel
if num < 0:
print("Sorry, you need to enter a postive number.")
return self.fuelLevel
if (self.fuelLevel + num) > self.fuelCapacity:
return self.getFuelLevel
if (self.fuelLevel + num) == self.fuelCapacity:
self.fuelLevel += num
return self.getFuelLevel
if (self.fuelLevel + num) < self.fuelCapacity:
self.fuelLevel += num
return self.getFuelLevel
def getFuelEfficiency(self):
return self.getCar()[0]
def getFuelCapacity(self):
return self.getCar()[1]
def getFuelLevel(self):
return self.getCar()[2]
def getOdometer(self):
return self.getCar()[3]
def tripRange(self):
numOfMiles = self.fuelEfficiency * self.fuelLevel
return numOfMiles
def __str__(self):
FE = self.getFuelEfficiency()
FC = self.getFuelCapacity()
FL = self.getFuelLevel()
OD = self.getOdometer()
string = '{}:{}:{}:{}'.format(FE,FC,FL,OD)
return string
这里的测试人员程序与测试是否正常运行相辅相成。如果一切都已修复,输出将为“#34;未发现错误&#34;
”from car import *
def main():
c = Car(25, 15)
checkNum(c.tripRange(), 0, 'Test 1')
expected = (25, 15, 0, 0)
checkCar(c, expected, 'Test 2')
c.addFuel(-1)
checkCar(c, expected, 'Test 3')
c.addFuel(1000)
checkCar(c, expected, 'Test 4')
c.addFuel('doctor')
checkCar(c, expected, 'Test 5')
c.addFuel(0)
checkCar(c, expected, 'Test 6')
c.addFuel(15)
expected = (25, 15, 15, 0)
checkCar(c, expected, 'Test 7')
c.drive(50)
expected = (25, 15, 13, 50)
checkCar(c, expected, 'Test 8')
c.drive(100000)
expected = (25, 15, 0, 375)
checkCar(c, expected, 'Test 9')
c.drive(5)
expected = (25, 15, 0, 375)
checkCar(c, expected, 'Test 10')
c.addFuel(10)
expected = (25, 15, 10, 375)
checkCar(c, expected, 'Test 11')
c.drive(-1)
expected = (25, 15, 10, 375)
checkCar(c, expected, 'Test 12')
c.drive(0)
expected = (25, 15, 10, 375)
checkCar(c, expected, 'Test 13')
checkNum(c.tripRange(), 250, 'Test 14')
if not errorsFound:
print('No Errors Found')
def checkCar(car, expected, message):
global errorsFound
mpg, cap, level, odo = expected
if car.getFuelEfficiency() != mpg:
errorsFound = True
print(message + ': Error efficiency. Expected ' + str(mpg))
print('\tCar:', car)
if car.getFuelCapacity() != cap:
errorsFound = True
print(message + ': Error capacity. Expected ' + str(cap))
print('\tCar:', car)
if car.getFuelLevel() != level:
errorsFound = True
print(message + ': Error level. Expected ' + str(level))
print('\tCar:', car)
if car.getOdometer() != odo:
errorsFound = True
print(message + ': Error odometer. Expected ' + str(odo))
print('\tCar:', car)
def checkNum(value, expected, message):
global errorsFound
if value != expected:
errorsFound = True
print(message + ': Error value. Expected {}. Got {}'.format(expected, value))
errorsFound = False
main()
答案 0 :(得分:0)
驱动方法中存在错误导致程序崩溃的原因
test above 9 car status:
fueleffiency = 25
fuel = 13
在测试9中c.drive(100000) 在驱动方法中,没有检查是否one_gallon&gt; self.fuellevel和那个 恰好发生在测试9中,one_gallon = 100000/25 = 4000,大于 试验后的燃料水平8 试试这个驱动
def drive(self,miles):
if miles < 0:
return ("The car is not driven")
one_gallon = miles / self.fuelEfficiency
if one_gallon < self.fuelLevel:
print("The car drove {} miles".format(miles))
elif self.fuelLevel == 0:
print("The car drove 0 miles")
elif one_gallon > self.fuelLevel:
print("car cannot drive") #or any overflow condition
self.fuelLevel -= one_gallon
self.odometer += miles
答案 1 :(得分:0)
这是一个drive()
方法,它实际上实现了您所说的内容:
Drive方法应该将汽车移动并指定它 作为参数的英里数量。如果汽车可以达到所有里程 没有燃料耗尽,汽车就行驶并输出 英里,同时也增加里程表。如果它不能使 旅行中仍然有油箱中的燃料,然后它开了很长时间 直到fuelLevel为0。
def drive(self, miles):
if miles < 0:
return # The car is not driven
trip_gallons = miles / self.fuelEfficiency
if trip_gallons <= self.fuelLevel:
self.odometer += miles
print("The car drove {} miles".format(miles))
self.fuelLevel -= trip_gallons
else:
miles_possible = self.fuelLevel * self.fuelEfficiency
self.odometer += miles_possible
print("The car drove {} miles".format(miles_possible))
self.fuelLevel = 0 # Out of gas
您可能需要考虑将checkCar()
的一些代码添加到errorsFound
,以便将汽车重置为预期值。这样,一次测试失败就会搞砸所有后续测试。