Creating Class Methods


#1



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


Error message printed:
Traceback (most recent call last):
File "python", line 12, in
TypeError: display_car() takes exactly 4 arguments (1 given)

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


I can't figure out what I did wrong here.
Adding the arguments on line 12 prints:
Traceback (most recent call last):
File "python", line 12, in
NameError: name 'self' is not defined


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." %(self.color, self.model, str(self.mpg))

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

print my_car.display_car()


#2

Because this is a method, it looks to self for all the attributes of the instance in current context. It is the only parameter. The others should be removed.


#3

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