10/11 Classes- Overriding methods


#1

My code produces the correct output but I get the error message from Codeacademy: "The only place the actual value of my_car's condition should be set is in the ElectricCar.drive_car() method; always use member variables to access such values.". Please can anyone help.

class Car(object):
    condition="new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def drive_car(self):
        self.condition ="used"
    def display_car(self):
        print "This is a "+str(self.color)+" "+str(self.model)+" with "+str(self.mpg)+" MPG."
class ElectricCar(Car):
    def __init__(self, battery_type,model, color, mpg):
        self.battery_type = battery_type
        self.model = model
        self.color = color
        self.mpg   = mpg
# Inside ElectricCar add a new method drive_car() that changes the car's condition to the string "like new".
    def drive_car(self):
        self.condition ="like new"
my_car = ElectricCar("molten salt","DeLorean", "silver", 88)
# Outside of ElectricCar, print the condition of my_car
print my_car.condition
# drive my_car by calling the drive_car() function
my_car.drive_car()
# print the condition of my_car again
print my_car.condition

#2

It wants you to pass the old init into the new init method.

Like so

class ElectricCar(Car):
    def __init__(self, battery_type,model, color, mpg):
        Car.__init__(model, color, mpg)
        self.battery_type = battery_type

This will allow you to work on the member object you inherit from the base class.


#3

Thanks. but I am still getting the same error.
I added the line
Car.__init__(self, model, color, mpg) to the electricCar class, I also deleted, the following lines from the electricCar class, since they are inherited from the car class
self.model = model
self.color = color
self.mpg = mpg
I am still getting the same error" The only place the actual value of my_car's condition should be set is in the ElectricCar.drive_car() method; always use member variables to access such values."