Indentation error on overriding methods problem in Classes


#1


Hi! I am new to coding and I'm sure there is something I am doing wrong that is simple to fix, but I cannot figure out where I am off on my indention. Can someone please help?

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

I am getting an indentation error message for line 30:

File "python", line 30
def drive_car(self):
^
IndentationError: unexpected indent

the indentation looks correct to me...so I am frustrated and cannot figure out how to make this work.

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, 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, model, color, mpg, battery_type):
        self.model = model
        self.color = color
        self.mpg = mpg            
        self.battery_type = battery_type
        
my_car = ElectricCar("Tesla", "black", 300, "molten salt")

    def drive_car(self):
        self.condition = "like new"
        
print my_car.condition
my_car.drive_car()
print my_car.condition


#2

@sflahive
You should put your Instantiating outside of your ElectricCar() class definition

Thus place

my_car = ElectricCar("Tesla", "black", 300, "molten salt")

outside, AFTER the class definition.


#3

thank you. i've made this mistake before and should have realized it.


#4

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