9. Inheritance



Hi! It keeps on telling me that I have entered 5 parameters and only four are needed, but I'm pretty sure I only entered four lol. I must have messed up somewhere

Shell error message:
Traceback (most recent call last):
File "python", line 25, in
TypeError: init() takes exactly 4 arguments (5 given)

CodeAcademy error message:
Oops, try again. my_car does not appear to be an instance of ElectricCar.

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." %(my_car.color, my_car.model, str(my_car.mpg))
    def drive_car(self):
        self.condition = "used"

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

print my_car.condition

print my_car.drive_car()

print my_car.condition

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type
my_car = Car("Volvo", "navy", 89, "molten salt")

Thanks! I appreciate any help!



you need to create an instance of ElectricCar and not Car :slight_smile:

my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")

The parent class's init method will be called in extended class, where it also take an extra argument of 'battery_type' argument. So simply you can call the child (extended) class and pass the arguments there.

simply print battery type using this and see
print my_car.battery_type


@corepro38901, do not post complete code without explanation, as this is against the community guidelines.


isn't that what the #comments in the code itself are doing? no disrespect intended, I just thought that was their purpose...


#This is the final code.
#prints molten sale

Aren't really considered an explanation.

How does one little note explain how the code works, and why you need certain things?


My bad, will remove it now. Thanks for the warning