7/11 Creating class methods


#1



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


Actually, I don't have any errors in the exercise, I just have a query.


Is there any way I can incorporate the variable "condition" in the string we have to print so that it reads "This is a new silver DeLorean with 88 MPG.


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


#2

yes you can:

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

my_car = Car("DeLorean", "silver", 88)
print my_car.display_car()

I am however slightly puzzeled by the syntax. Because you never pass condition to the init function, and this answer doesn't really explain why this works


#3

Well, my bad. I did the same thing but used "condition" instead of "self.condition". Thank you!


#4