class Car(object): condition = "new" def __init__(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg def display_car(self): MPG = str(self.mpg) return "This is a %s %s with %s MPG." %(self.color, self.model, MPG) def drive_car(self): self.condition = "used" my_car = Car("DeLorean", "silver", 88) condition = drive_car(self) print condition print my_car.condition.drive_car() # what 's wrong with my code? ''' Traceback (most recent call last): File "python", line 16, in <module> NameError: name 'drive_car' is not defined '''
Modifying Member Variables
condition = drive_car(self)
drive_caris a method defined on an object, hence, you can only call it by doing
drive_cardoes not return anything, so your doing
condition = drive_car(self)does not make sense.