Creating Class Methods


#1

https://www.codecademy.com/courses/learn-python/lessons/classes/exercises/creating-class-methods?action=lesson_resume

File “python”, line 12, in
File “python”, line 8, in display_car
NameError: global name ‘color’ is not defined

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

#2

That line should not be inside the class block. Move it down and out to the left margin.


#3

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