Creating class methods


Traceback (most recent call last):
File "python", line 11, in
AttributeError: 'Car' object has no attribute 'display_car'

Oops, try again. Make sure your Car class has a display_car() method.

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" + self.color + self.model + "with" + str(self.mpg) + "MPG"

print my_car.display_car()



before you can use print my_car.display_car()
you have to creat my_car:

my_car = Car("DeLorean", "silver", 88)

I don't know why you are gettig this error instead of 'my_car' is not defined.

Also check the formatting of the string display_car() is supposed to print.


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