Creating class methods


#1



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


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()


#2

Hi,

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.


#3

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