"Oops, Try Again" In Creating Class Methods



Oops, try again. Make sure you pass the self keyword to the display_car() method.

I expected my code to be correct.

Replace this line with your 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, model, color, mpg):
       print "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))

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

my_car.display_car("DeLorean", "silver", 88)


That error message is poorly phrased to say the least (for starters, self is not a keyword, and it's also not something to be explicitly passed to the method), but the problem is that you're giving your car's display method information that it already has access to.


When I took out the arguments when I tried to display the car, I still got the same message AND this from the command prompt:

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


Then you would need to read that error message and compare what it says to what's supposed to be.
What's supposed to be? What does that error message say?


Yes, it says "display_car() takes exactly 4 args (1 given)" That makes me think I would need to put in the model, color, and mpg as arguments, since that + self is 4 arguments. Either way, it doesn't solve the original problem.


If you're supposed to give it no arguments, then how many arguments should it be expecting? How many is it expecting? What's the difference then between should and is?

You have to base your idea of what's correct on what behaviour things should have, not on what they currently are. You've already established that things are not as they should be.


I'm sorry, I think there was a misunderstanding. I thought you meant I
should put in 4 arguments for the creation of the display_car() method, but
then not put it any when I called it. I now get that you meant not put in
any args (besides self) when I made display_car().


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