Create Class Methods>>>>HELP ME


#1

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

WHAT IS WRONG?


#2

Solved it myself

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 " + my_car.color + " " + my_car.model + " with " + str(my_car.mpg) + " MPG."
my_car = Car("DeLorean", "silver", 88)
print my_car.display_car()

Remember to put underscores at (def init)


#3

display_car() is a method of the Car class so has access to self if we include it in the parameter.

def display_car(self):
    return " ... " % (self.color, self.model, str(self.mpg)

This may have let you pass but it is not the solution. What happens when we create another instance?

your_car = Car( ... )
your_car.display_car()    # ???

None of the returned string will match your_car since you have my_car hard coded. This will also raise an exception if my_car does not exist.


#4

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