Initializing A Class


My code does not return what i want but i still pass. what is wrong?

``` class Car(object): condition = "new" def __init__(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg

model = "delorian"
color = "silver"
mpg = 88
my_car = Car(model, color, mpg)
print my_car

<do not remove the three backticks above>


In order to print using the direct call you have, your class will need a representation method…

  def __repr__(self):
    return "This is a %s %s with %d MPG" % (self.color, self.model, self.mpg)

Notice the slight difference in the following two methods…

  def display_car(self):
    print "This is a %s %s with %s MPG." % (self.color,self.model,str(self.mpg))
  def __repr__(self):
    return "This is a %s %s with %d MPG." % (self.color, self.model, self.mpg)

and the way they are called…

print my_car


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