7/11 Creating Class Methods


#1

I understand how the answer is supposed to be coded, (using %s in the string) but I used a different solution that returns the same output but the editor says it is wrong. I was just wondering which way is better and/or easier to read.

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

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", my_car.color, my_car.model, "with", my_car.mpg, "MPG."

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

#2

Because the print wants you to use %s. That's why it's telling you it's wrong despite output is as expected.

Also, doing it as return "This is a %s %s with %s MPG." %(self.color, self.model, str(self.mpg)) looks far neater. Imagine you have to do that for a whole page?


#3

What is wrong with my code? It says no attribute display_car exists but I believe it does.

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 %s MPG." %(self.color, self.model, \
        str(self.mpg)))

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


#4

have the same problem:
class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg

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


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


print my_car.display_car()

#5

Maybe it's because you should return-ing rather than print here.

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


#6

Please post well formatted quotes and ensure you directly reply to me so I could help.

Thank you! :slight_smile:


#7

You can call a return statement with a string as long as you call it with PRINT like I did.


#8

Well, I never said you can't.

I'm suggesting if that's what's keeping you from passing that exercise.

Moreover, codecademy uses python2 IDE and not python3. Just to mention. I see you use a bracket for the print.


#9

"." after MPG put a full stop thats a mistake .. :stuck_out_tongue:
class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def display_car(self):
return "This is a %s %s with %s MPG."%(self.color,self.model,str(self.mpg))

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


#10

Haha... Cc: @skillville


#11

thanks for ponting out how a single dot can change everything LOL, that fixed my problem, thanks!


#12

Thanks @deadlyhacker even i had the same problem of the full stop after MPG. Fixed That


#13

I had the same issue. I fixed it by putting a period behind the MPG

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

Try this


#14

Good grief. Thank you for that. Had the code perfect on first try and a period just shot it all down haha


#15

It works for me!

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car (self):
        return ( "This is a %s %s with %s MPG."%(self.color, self.model, self.mpg))
    
my_car = Car("DeLorean", "silver", 88)

print my_car.display_car()

#16

Yep, I did the same thing. Wasn't clear to me that you had to use substitution. Pity the interpreter isn't smart enough to realize you actually did print out the statement.

Glad someone else did the same thing so I could figure out why it wasn't accepting my code!


#17

Thanks for this heads up, though a shame it doesn't mark the output. Plus am not sure how using substitution would be easier for a complex text than using concatenated strings with variables between. I'm thinking of how mail merge works and can't imagine having spaces and then an index at the end would be easier than having them in-line. Appreciate any feedback or insight on this!


#18

return "This is a", my_car.color, my_car.model, "with", my_car.mpg, "MPG."

you can't succee with your code above,because the result it return is a list datatype ,but not a string the test expect for !

you can only use one of the two code below to pass it:
return "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))
or
return "This is a "+self.color+" "+self.model+" with "+ str(self.mpg)+" MPG."


#19

I didn't realize it required us to use %s no mention of that in the description or the hint ( I suppose we are just expected to know that like most of these lessons).