9. Inheritance question


#1



https://www.codecademy.com/en/courses/python-intermediate-en-egNXj/1/3?curriculum_id=4f89dab3d788890003000096#

i dont understand what is wrong with the code, and the hint is not too clear for me to see, i just dont know what to do

i got the
Traceback (most recent call last):
File "python", line 18, in <module>
TypeError: __init__() takes exactly 4 arguments (5 given)

error

thanks for any 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):
        return "This is a %s %s with %s MPG." %(self.color, self.model, self.mpg)
        
    def drive_car(self):
        self.condition = "used"
        
class ElectricCar(Car):
    def __init__(self, battery_type):
        self.battery_type = battery_type

my_car = Car("DeLorean", "silver", 88, "molten salt")
print my_car.condition
my_car.drive_car()
print my_car.condition


#2

Take "molten salt" out of the Car instance and just have " my_car = Car("DeLorean", "silver", 88) ".

Then make sure the init function has a model, color, mpg, and battery_type.
-> init(self,model,color,mpg,battery_type)

When you create an instance for ElectricCar, do it like this "my_car = ElectricCar(the model ,the color ,the mpg , the battery type)"

I hope I made it clear enough and good luck :smile:


#3

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

my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")
print my_car.condition
my_car.drive_car()
print my_car.condition

like that?
i tried and didn't worked :stuck_out_tongue:
anyways, thanks for the help :slight_smile:


#4

this is mine :slight_smile:
def init(self,model,color,mpg,battery_type):
self.model = model
self.color = color
self.mpg = mpg
self.battery_type = battery_type
def drive_car(self):
self.condition = "like new"
print ElectricCar.condition
my_car = Car("DeLorean", "silver", 88)
my_car = ElectricCar("bmw","color",150,"molten salt")


#5

alright then, but here we have the inheritance behind right?


#6

Correct, the inheritance behind!


#7

This does not really inherit anything since all the properties are assigned in the subclass. We can pass the parameters up to the super class so we use the inherited properties initialized for this subclass instance.

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

#8

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