Creating Class Methods


#1



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


Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG.".


It prints what is expected.
Console:
This is a silver DeLorean with 88 MPG.
None


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", str(self.mpg), "MPG."

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


#2

Perhaps you're expected to create a string and print it all at once, as opposed to printing multiple strings. The output would be the same but may look different to codecademy due to a hackish approach to monitoring output


#3

Thanks for your response.

I tried the following too.

def display_car(self):
        print "This is a", 
        print self.color, 
        print self.model, 
        print "with", 
        print str(self.mpg), 
        print "MPG.

def display_car(self):
        print "This is "+ self.color+" "+ self.model+ " with " + str(self.mpg) + " MPG."

None of the above works. I understand the problem with codeacademy's approach. I just want to get to the next step!


#4

That prints a slightly different string.


#5

Okay, but none of the three gets me past to the next step. What is the right print command?


#6

After reviewing your code it appears that you are missing the "a" after "This is" in your print string statement and also the "G" in "MPG" at the end of the statement. The code below worked for me.

def display_car(self):
print "This is a " + self.color + " " + self.model + " with " + str(self.mpg) + " MPG."


#7

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