Overiding Methods


#1

Hiya folks, My code is not being accepted by codecademy with the comment " Oops, try again. You should first display the 'new' condition of my_car.". I am not sure I understand what it means. I did alter my code but that did not help, so I reverted tothis code On the console I get
like new
None
like new
None
Thank you


class Car(object):
    condition = "new"
    
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    
    def  display_car(self):
        print "This is a %s %s with %s MPG." %(self.color, self.model, self.mpg)
        
    def drive_car(self):
        self.condition = "used"
        return self.condition  
class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.model=model
        self.color = color
        self.mpg = mpg
        self.battery_type=battery_type
    def display_ElectricCar(self):
        print "This is a %s %s with %s MPG and %s type battery." %(self. model, self.color, self.mpg, self.battery_type)
    def drive_car(self):
            self.condition = "like new"
            
my_car = ElectricCar("DeLorean", "orange", 808, "molten salt")
my_car.drive_car()

print my_car.condition
print my_car.drive_car()
print my_car.condition
Replace this line with your code.


#2

This method acts upon a class variable so needs to return value.

Are you being asked to print the condition attrtibute on a Car instance, before driving it?

Also,

There is nothing to print; simply run the method.

I don't recall there ever being a need for a display_ElectricCar method.

Please post a link to the exercise you are on. Thanks.


#3

Thank you. I modified the code as you suggested ie
def drive_car(self):
self.condition = "like new"
return self.condition

and it worked. Thanks
The instruction, (below), did ask for print the condition of my_car twice. I am not sure why

Inside ElectricCar add a new method drive_car() that changes the car's condition to the string "like new".
Then, outside of ElectricCar, print the condition of my_car
Next, drive my_car by calling the drive_car() function
Finally, print the condition of my_car again

#4

This method does not need to return anything. The change takes place on the class variable, internally.


#5

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