Creating class methods


#1



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

Oops, try again. Make sure you pass the self keyword to the display_car() method.


It prints the sentence in order and looks fine, but it still says '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,model,color,mpg):
        return ('This is a %s %s with %s MPG.' %(color,model,mpg))

# Instances and printing 
my_car = Car("DeLorean", "silver", 88)
print my_car.condition
'''
print my_car.model
print my_car.color
print my_car.mpg
'''
print my_car.display_car(my_car.model,my_car.color,my_car.mpg) # call display_car with arguments


#2

The above is where your issue is. All the above variables are already defined when the Car object is made (see __init__), the error is telling you to reuse them.


#3

Is a method of the class and takes only one parameter, self. Inside the method we will use instance variables defined in __init__ as explained above.


#4

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