我正在定義一個名爲'car'的類,我將它與一個對我的類運行一系列測試的文檔進行比較。但是,我收到錯誤,我不知道爲什麼。這是我的代碼。方法Drive
應該採取汽車並將其移動指定的英里數。如果汽車能夠在給定的燃料量下達到所有的里程數,那麼汽車就會啓動並輸出英里數。如果它不能,它會達到最大的里程數。 addFuel
方法應該爲汽車添加燃料,但是如果它溢出,它不會添加任何燃料,並且如果指定的參數不是整數,或者不是正值,則應該拋出異常。並且tripRange
模塊應該給出車內的加侖數量,並確定您將行駛多少英里。下面我已經發布了我的代碼,然後是代碼來測試它。你可以幫我嗎?我很感激。面向對象的Python編程
#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")
milesDriven = miles/self.fuelEfficiency
if milesDriven < self.fuelLevel:
print("The car drove {} miles".format(miles))
else:
if self.fuelLevel == 0:
print("The car drove 0 miles")
else:
newMiles = milesDriven * miles
print("The car drove {} miles".format(newMiles))
self.fuelLevel = self.fuelLevel - milesDriven
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:
raise KeyError("String valued enter, an integer was expected.")
if num < 0:
print("Sorry, you need to enter a postive number.")
if num + self.fuelLevel > self.fuelCapacity:
return self.fuelLevel
else:
return self.fuelLevel + num
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
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
而這裏的測試代碼:
from car import *
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))
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')
errorsFound = False
main()
*我收到錯誤*什麼錯誤?有錯誤消息嗎?哪個測試用例失敗? –
你得到什麼樣的錯誤?看起來有縮進錯誤,但它們可能是因爲你如何複製粘貼代碼到這裏。如果它們不是,請注意任何沒有縮進的def函數都在類的外面,因此不是一個方法,而是一個正常函數,即使它有一個「self」參數。 – Dleep
這一切似乎運行良好,直到它到達addFuel方法。當測試文件給參數一個字符串時,我不知道如何拋出異常並移過它。另外,我只是得到錯誤的答案。這一切都有效,但我不知道我做錯了什麼來得到錯誤的答案。 –