I am defining a class called 'car' I am comparing it to a document that runs a series of tests on my class. However, I am getting errors, and I am not sure why. Here's my code. The Drive method is supposed to take the car and move it a specified amount of miles. If the car can achieve all the miles with the given amount of fuel, then the car makes the trip and outputs the miles. If it can't, it goes the maximum amount of miles it can. The addFuel method is supposed to add fuel to the car, but if it overflows it doesn't add any fuel, and if the parameter specified isn't an integer, or isn't a positive value, it's supposed to throw an exception. And the tripRange module is supposed to be given the amount of gallons in the car, and determine how many miles you will travel. Below I have posted my code first, and then the code to test it out. Can you help me? I appreciate it.
#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
And here's the test code:
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()
selfparameter on it.test.py.c.addFuel('doctor')