Creating class methods (Str conversion issue)


#1

Issue:
I don't understand the problem I even tried using str(self.mpg)

Error thrown:
Traceback (most recent call last):
File "python", line 13, in
File "python", line 9, in display_car
TypeError: %d format: a number is required, not str

Oops, try again. You can use str() to change mpg to a string as an easy way to add it to the display text.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = str(mpg)
        
    def display_car(self):
        print "This is a %s %d with %s MPG."%(self.model, self.color,self.mpg)

my_car = Car("DeLorean", "silver", 88)
print my_car.condition
my_car.display_car()
Replace this line with your code.


#2

Error:

TypeError: %d format: a number is required, not str

see here

my_car = Car("DeLorean", "silver", 88)

silver is a string value not number .

but here

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

you used %d for self.color.


#3

Thank you! I did not see that error


#4

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