10. Problem


#1

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"

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("DeLorean", "silver", 88, "molten salt")

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

The error says that ElectricCar object has no attributes.
If someone could help me on this one it would be great, thank you!


#2
Traceback (most recent call last):
  File "python", line 24, in <module>
AttributeError: 'ElectricCar' object has no attribute 'conditon'

Is this the error you get? If it is then it is because in line 24 you put conditon and not condition.

Below is how your code should look like once it is fixed:

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"

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("DeLorean", "silver", 88, "molten salt")

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

Code and Syntax Highlighting


#3

I feel like the addition of

Car.init(self, model, color, mpg)

was never discussed on code academy, so I don't understand that line at all


#4

As far as I understand, by Car.__init__() you are just copying the __int__() method from the Car class instead of having to type the variables and values (self.model = model, self.color = color, and self.mpg = mpg) again. Notice that when the Car.__init__() method is called in the ElectricCar class four arguments are given (self, model, color, and mpg) since those four arguments are required in the __init__() method in the Car class to do its job.


#5

A post was split to a new topic: Code works but looks not quite right