Creating class methods


#1



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


it print perfect result : This is a silver DeLorean with 88 MPG.
None
BUT NO GREEN TO PASS AND THE ERROR MESSAGE : oops , make sure your Car class have display_car methode


class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg

    my_car = Car("DeLorean","silver",88)
    
    def display_car(self):
        return "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)
    
    print my_car.display_car()


#2

Try it with

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)

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

print my_car.display_car()

and see
- instantiating CAR() within the class definition is a bad idear....
- take notice of the indentation usage, its very important in PYTHON !!!


#3

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