Classes 7/11 Codecademy mistake or...?


#1

So the instructions go like this:

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." You can use the str() function to turn your mpg into a string when creating the display string.

Replace the individual print statements with a single print command that displays the result of calling my_car.display_car()

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

And the Codecademy solution:

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

They told us to:

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

But they did this:

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

Does it mean that both ways work or did Codecademy made a mistake, because even the discuss link tell us that we should RETURN the string inside def display_car , and then PRINT my_car.display_car() ?


#2

i would personally use print inside the function, given the purpose of the function is to print.

looking at old forum posts, print was always used within the function. Seems they change the instructions, but not the get solution.


#3

Ok so its better to do it like the Codecademy solution


#4

given the sole purpose of the function is to print (display something), yea print would be better. Return adds no value here, but that is for this specific scenario, not in general.


#5

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