7. Creating class methods


#1



https://www.codecademy.com/courses/python-intermediate-en-egNXj/1/1?curriculum_id=4f89dab3d788890003000096#


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()


#2

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.


#3

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.


#4

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)
my_car.display_car()

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


#5

What wrong in here ?


#6

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!


#7

Hey,

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.


#8

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