Creating class methods - mpg not converting to string


#1

In the creating class methods exercise, I keep getting this error message:

Traceback (most recent call last):
  File "python", line 11, in <module>
  File "python", line 8, in display_car
TypeError: not all arguments converted during string formatting

But in my code, I use str() with mpg…do I have it in the wrong place? Or is something else wrong in my 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):
    return "This is a %s %s with &s MPG." %(self.color, self.model, str(self.mpg))

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

#2

The & should be a %.

It may be in the instructions to use the str() function, so that would be okay; but, we can let the formatter convert to string by specifying the data type of the variable to go into that field.

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

#3

Wow, I don’t know how I missed that…my brain must not be working after coding for a few hours haha. Thank you for the help!


#4

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