Error when calling instance of child class ElectricCar


#1


I am getting an error when trying to call an instance of the child class ElectricCar. What am I doing wrong?

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


CONSOLE ERROR:
TypeError: init() takes exactly 2 arguments (5 given)

AND CODECADEMY ERROR MESSAGE:
my_car does not appear to be an instance of ElectricCar.

CODE:

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,battery_type):
        self.battery_type = battery_type

my_car = ElectricCar("Bronco", "orange", 8, "molten salt")


#2

This is the line that is raising the exception. Two arguments accepted

my_car = ElectricCar("Bronco", "orange", 8, "molten salt")

Five arguments given.


#3
class ElectricCar(Car):
    def __init__(self,battery_type):
        self.battery_type = battery_type

The issue is in the above. The function you created gives room for two and you're calling five parameters in it.


#4

We still need to initialize the Car class.

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type
        
my_car = ElectricCar("prius", "red", 99, "molten salt")

#5

Why initialize Car class when you could just:

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

    my_car = ElectricCar("molten salt", "Volvo", "red", 90)

:smile:


#6

That's splitting hairs. The main thing is to initialize the super class instance. How we do it could be in any number of ways, one supposes.


#7

Okay, that's fine! :smile:


#9