10. Overriding methods _ Don't know how to use the child class


#1

Instructions
1. Inside ElectricCar add a new method drive_car() that changes the car's condition to the string "like new".
2. Then, outside of ElectricCar, print the condition of my_car
3. Next, drive my_car by calling the drive_car() function
4. Finally, print the condition of my_car again

I want to do 3, 4 but can't. Don't know what concept I am missing. It's quite stressful and keep coming back to the same point.

class Car(object):
        condition =  "new"
        def __init__(self, model, color, mpg):
            self.mode = model
            self.color = color
            self.mpg = mpg
    
        def display_car(self):
            print ("This is a %s %s with %s MPG" % (self.model, self.color, self.mpg))
    
        def drive_car(self):
            self.condition = "used"
    
    class ElectricCar(Car):
        def __init__(self, model, color, mpg, battery_type):
            Car.__init__(self, model, color, mpg)
            self.battery_type = battery_type
    
        def drive_car(self):
            self.condition = "like new"
    
    my_car = ElectricCar("Lexus", "black", 90, "molten salt")
    
    print (my_car.condition)
    my_car.drive_car.ElectricCar()

#2

remove the ".ElectricCar()" on the last line... and maybe the parenthes on the second last


#3
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.model, self.color, self.mpg))

    def drive_car(self):
        self.condition = "used"

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

    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)

#4

so the point for me is that I have to put my ElectricCar object (my_car) before the print command, and the green button should comes up...thanks medouxa :grin:


#5

I'm not sure If I'm reading this correctly,
the main difference between medouxa's before and after code is that the child class (ElectricCar) was initially called within the parent class, so in order to fix this just delete the spaces before the ElectricCar object to move it out of the Car class? Sorry for my English


#6

in here they want you to understand the inheritance concept ...look at condition is exist in ElectricCar class cause it inherited condition member variable without re identified it again


#7

So i have triple checked my code, and it looks just like yours but it keeps telling me that i didn't update condition to my_car "like new" and i have. x.x


#8

what is wrong with this? ...

class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg

def display_car(self):
    return "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))

def drive_car(self):
    self.condition = "used"

my_car = Car("DeLorean", "silver", 88)

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

class ElectricCar(Car):
def init(self, model, color, mpg, battery_type):
self.battery_type = battery_type
return super(ElectricCar, self).init(model, color, mpg)
def drive_car(self):
self.condition = "like new"

my_car = ElectricCar("Prada", "Blue", 90, "molten salt")
print my_car.condition
my_car.drive_car()


#9

Hi,
If you run the "print my_car.condition" statement before calling the drive_car method then the "you havn't updated...." error will be raised.Hit "save&submit" after the second print **statement, there will be no error.

thanks

pkjee