Creating class methods


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

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


not all arguments converted during string formatting

I am not understanding the mistake. Can someone guide me?


The issue is that you are joining three bits together. In the first string, you are using formatting (%s); however, you are trying to tell it what that formatting is on the last string (%(self.model,self.color)).

To fix it as it currently is, do the following:

        return "This is a [%s] [%s] with "%(self.model,self.color) + str(self.mpg) + "              MPG"

That said, I would be inclined to remove the whole "+ str(self.mpg) +" part and use the same string formatting you already have used i.e.:

        return "This is a [%s] [%s] with %s              MPG"%(self.model,self.color,self.mpg)

Or with the newer .format():

        return "This is a [{}] [{}] with {}              MPG".format(self.model,self.color, self.mpg)

Just cleaner and easier to read in my opinion.


Thanks a lot. It worked!!


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