7. Creating class methods



Traceback (most recent call last):
File "python", line 12, in
TypeError: display_car() takes exactly 2 arguments (1 given)

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

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


From what i experienced. I don't think you need str in def display_car(self,str): and you should take a look at your return statement cause there is a couple errors in there.


Try adding some spaces to your return statement otherwise the checker will not pass it and also remove the str argument form display_car as
1. str is reserved as a function name
2. It is not needed.


My code is like this:

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" , self.color , self.model , "with" , self.mpg , "MPG."

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

However i get the following error: Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG." even though that is exaclty what comes out

Not sure how to format as the first person did, but my indents are all correct just not in here


What wrong in here ?


I believe it should be: return "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg)), not print.
Also, note that you have to convert the MPG to string by using str()
Hope this helps!



Thanks for the response.

I tried the change and the O/P is good with other IDE's just the Codecademy editor is not letting me through.


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