10. Overriding methods


#1

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

The code below is returning the following error: TypeError: drive_car() takes no arguments (1 given)

I cannot for the life of me decipher why. The final three lines of code are in fact a copy/paste from lesson 8 of this section, where it worked just fine.

Any input would be much appreciated.

EDIT: just noticed that in the child class, condition should be self.condition, and I corrected that, but it's still returning the same error.

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 " + self.color + " " + self.model +" with " + str(self.mpg) +" MPG."
    def drive_car(self):
        self.condition = "used"
    
class ElectricCar(Car):
    def __init__(self, battery_type, model, color, mpg):
        self.battery_type = battery_type
        super(ElectricCar, self).__init__(model, color, mpg)
    def drive_car():
        condition = "like new"

my_car = ElectricCar("molten salt", "Dodge","green", 100)

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

#2

You forgot to call this line with self. before it.

And,

Your supposed to create one normal car and one electric car so your code would be like,

my_car = Car("DeLorean", "silver", 88)
my_car = ElectricCar("Pagani Huayra", "red", 99, "molten salt")
print my_car.condition
print my_car.drive_car()
print my_car.condition

The values are just examples.


#3

I found the problem: I had def drive_car(): instead of def drive_car(self):, which fixed it. Thanks for the input!


#4

A post was split to a new topic: 10. overriding methods


#5