7.Creating Class Methods


#1



7.Creating Class Methods


My code does print "This is a silver DeLorean with 88 MPG." like it's asked, but it still prints " Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG.", I have no idea why.


Replace this line with your 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):
        print "This is a",self.color,self.model,"with",self.mpg,\
        "MPG."
my_car = Car("DeLorean", "silver", 88)
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg
my_car.display_car()


#2

Hi, @nathan.cournima ,

Use string formatting, with either the % operator or the format method, to create the output. That will create a single unified string, which the submission correctness test (SCT) requires for this exercise. Though your output looks identical to what is specified in the instructions, it does not suffice.


#3

Just tried that, works like a charm thanks for the info !


#4

seems like a problem with the SCT, not his code though


#5

Hi @jacobdickey_2016 ,

Quite likely, the author of the course intended for users to assemble a unified string via either concatenation or string formatting. In that case, the remedy for this problem would be for the instructions to be clarified to ask for either of those techniques explicitly.


#6

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


#7

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

it's output is :
This is a silver DeLorean with 88 MPG
None

but it's showing me an error ->Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG.".

I'm getting the correct output then why it's showing me this error?


#8

leaving out the dot after MPG i.e. MPG instead of MPG. causes the issue :slight_smile:

Coding is amazing isn't it? :smiley:


#9

haha..agree..thanx a lot..:grin: it worked


#10

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