10. Overriding methods


#1



I keep getting this error:
Oops, try again. It looks like you didn't update the condition of my_car to 'like new'.

I cant seem to find the problem with the code. Please Help..


class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self):
        print "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))
    
    def drive_car(self):
        self.condition = "used"
        
class EletricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.battery_type = battery_type
        super(EletricCar, self).__init__(model,color,mpg)
        
        
    def drive_car(self):
        self.condition = "like new"

my_car = ElectricCar("Lexus", "black", 90, "molten salt")

print (my_car.condition)
my_car.drive_car()
print (my_car.condition)


#2

Your car class has a "condition" attribute ("new"), why your ElectricCar doesn't have it? :wink:


#3

I thought it would inherit the member variables of Car?


#4

Sorry, you're right! I'm gonna take a risk here because I'm not 100% sure, but maybe inside drive_car(self) you shouldn't use self.condition but super?

I apologise, I'll learn some too when someone more instructed helps us both here. :blush:


#5

I found the error. I was missing a C in Electric.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.