Creating class methods


#1

Continuing the discussion from 7. Creating Class Methods:



Replace this line with your code. 
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)
my_car.display_car()


#2

What's the problem? It doesn't give any error for me.


#3

print "This is a " + self.color  +  self.model + " with " + str(self.mpg)  + " MPG."

You can write this line like this as well.


#4

FYI: Without the + " " + between self.color and self.model this will not quite print correctly.

print "This is a " + self.color  + " " + self.model + " with " + str(self.mpg)  + " MPG."

#5

Actually it will note the extra space here."This is a " :slight_smile: