7. Creating Class Methods


#1

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

My code is as follows:

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

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

However the console reads:
This is a silver DeLorean with 88 MPG
None

How can the error say that I did not print This is a silver DeLorean with 88 MPG when it is clearly written out on the console?


Creating class methods
#2

i think all you have to do is te replace print 'This is a ' + self.color + ' ' + self.model + ' with ' + str(self.mpg) + ' MPG' by return 'This is a ' + self.color + ' ' + self.model + ' with ' + str(self.mpg) + ' MPG'

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, "This is a [color] [model] with [mpg] MPG."
return not print


#3

I just applied your advice, yet the program returns the exact same error.


#4

can you guess that error is from one point !!
MPG.
return 'This is a ' + self.color + ' ' + self.model + ' with ' + str(self.mpg) + ' MPG.'


#5

AAAAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!
What? That one period is what caused the error?
(It worked BTW)


#6

Yeah , Never underestimate a single point


#7

I tried:

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

and got this error message:

Traceback (most recent call last):
File "python", line 18, in
File "python", line 9, in display_car
TypeError: not enough arguments for format string

line 18 is just: print my_car.display_car()
line 9 is the print statement above where I used the %s

Why?


#8
def display_car(self):
        print "This is a %s %s with %s MPG." % (  self.color,self.model ,self.mpg)

this how we can print string with arguments


#9

Please create your own topic with code + error + lesson
thanks