Creating Class Methods


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


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


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