Creating class methods


#1



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


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


Why is my code not working, when I have already passed the arguments of model,color and MPG while I initialised my_car to the class 'Car'?


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 %s %s with %d MPG", self.model, self.color, self.mpg)

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

my_car.display_car()


#2

print ("This is a %s %s with %d MPG", self.model, self.color, self.mpg)
1.You missed dot after MPG, %d (is okay)
2. Read the instruction to find what you have to do in next lines


#3

Even after adding the dot after the MPG, this is the output I am getting.

('This is a %s %s with %d MPG.', 'DeLorean', 'silver', 88)
None


#4

print ("This is a %s %s with %d MPG", self.model, self.color, self.mpg)

Back to problem code...

  1. You have to return.
  2. You are trying to print model first then color..i.e is self.model, self.color
    it should be self.color, self.model
  3. after this line "This is a %s %s with %d MPG." you have used comma not % to insert the values.
  4. I already mentioned the dot problem

There is one more problem...
I hope you find that... if not ..reply.


#5

There are multiply ways to concat string and variable:

x = "world"
print "hello", x
print "hello %s" %s

you seem to mix them. Please look at the exercise which approach you should use

don't use parentheses in for print (in python2, which codecademy uses), this might cause a tuple (a immutable list)

for the rest, following @greentreemee suggestions


#6

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

With the above print statement, the print output looks correct to me, but I am getting an error that says:

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


#7

Is the other problem converting 88 to a string?


#8

Why does codeacademy choose to use Python 2?


#9

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

Okay this line we have to debug it,Its root of our error.
lets see...

  1. Eraseprint

    Rule no 0.
    Properly read the instructions .

2.Lets see you code again...
("This is a %s %s with %d MPG", self.model, self.color, self.mpg)

When you insert something inside a string you do something like this

"I am a string..You're %s." % ("makokam")
It will return this string...
I am a string..You're makokam.

Can you see this?


#10

Greentreemee I know this is a easy problem, but I am stuck on this for a long time. :sweat:

After replacing 'print' with 'return', I am still not getting the correct output. Please find the screenshot below. Please help.


#11

Greentreemee I know this is a easy problem, but I am stuck on this for a long time. :sweat:

Its not easy or hard! we will solve it together,dont worry! :thumbsup:
Post your code after return in raw form here!

You're on right path,just few steps more!

Update**

try to get back at this...
return "This is a [color] [model] with [mpg] MPG."


#12

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