Creating class methods


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


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.


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.


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