Creating class methods


#1

This is my code

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()
print "This is a silver DeLorean with 88 MPG"

this is the ouptut:

This is a silver DeLorean with 88 MPG
This is a silver DeLorean with 88 MPG
None

and this is the error message:

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

the second print is a copy of the expected answer of the error message. I do not have a clue of what I do wrong.


#2

a . is missing at the end of your string, after MPG


#3

Thank you it changes the error message to:

Oops, try again. The only place the actual values of my_car should appear is when it is created; always use member variables to access these values.


#4

I had the same problem - in the string that you're telling it to return it needs to end the sentence with a full stop - not an error with your code, just with matching what Codecademy wants it to read as!
"This is a %s %s with %s MPG." (the full stop just before the " at the end)
Hope that makes it clearer


#5

@caitlinpb explains with a bit more words, what i was trying to explain


#6

This is the code:

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

The print works but I receive this error message :slight_smile:

Oops, try again. The only place the actual values of my_car should appear is when it is created; always use member variables to access these values.


#7

your code works fine? Try refreshing the page/a different browser. The enviroment might be sending old dat


#8

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