Creating class methods - error when print is correct


#1

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

I printed exactly what was requested but get this message - Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG."

Why?...

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

#2

from the instructions:

Inside the Car class, add a method named display_car() to Car that will reference the Car's member variables to return the string

the important word here is return, which means inside your display_car method should return the string by using the return keyword, but using , + return will give a tulpe, you want a string.

you will have to use a different approach, either convert the tuple into a string or try using %s or .format() to solve the problem


#3

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