1

I have been over this code many times and i am not able to find out why does the error keep saying

    print("This car has a " + str(self.battery_size) + "-kWh battery.")
AttributeError: 'Electric' object has no attribute 'battery_size'
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        long_name = str(self.year) +' '+self.make.title()+' ' + self.model.title()
        return long_name.title()

    def read_odometer(self):
        print("the car ran " + str(self.odometer_reading) + " miles ")

    def update_odometer(self, milage):
        self.odometer_reading += milage
        print("odo meter reading is "+str(self.odometer_reading))

class Electric(Car):
    def _init_(self, make, model, year):
        super().__init__(make, model, year)
        self.battery_size = 70

    def describe_battery(self):
        print("This car has a " + str(self.battery_size) + "-kWh battery.")

my_tesla = Electric('tesla', 'model s', '2016')
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()

Any help would be greatly appreciated. Thanks

norok2
  • 21,682
  • 3
  • 59
  • 88

1 Answers1

4

Should be:

class Electric(Car):
    def __init__(self, make, model, year):

with two underscores.

norok2
  • 21,682
  • 3
  • 59
  • 88
  • @InderjeetSingh If the constructor is not named correctly as `__init__`, it will not get executed during construction. Fixing the name, will result in the constructor being correctly called and hence `battery_size` attribute will be correctly assigned and it will be available when you will call `.describe_battery()`. – norok2 Oct 24 '19 at 00:03