8. Modifying member variables _ how to call the `drive_car()` method?


#1
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))
   
    def drive_car(self):
        self.condition = "used"
        
my_car = Car("DeLorean", "silver", 88)
# print my_car.condition
# print my_car.model
# print my_car.color
# print my_car.mpg

print my_car.condition

These are the instructions:
Instructions

  • Inside the Car class, add a method drive_car() that sets
    self.condition to the string "used".
  • Remove the call to
    my_car.display_car() and instead print only the condition of your
    car.
  • Then drive your car by calling the drive_car() method.
  • Finally,
    print the condition of your car again to see how its value changes.

I keep getting the message below, and don't know how to fix it.

Oops, try again. You didn't display the 'used' condition of the car after drive_car().


#2

the drive_car method is a part of the Car class meaning you can call it on an instance of the class and in this case my_car

my_car.drive_car()

#3

Can you please write me the whole code of the step?


#4

Here it is with comments

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))
   
    def drive_car(self):
        self.condition = "used"
        
my_car = Car("DeLorean", "silver", 88)

#the car's condition is new
print my_car.condition

#now dirve the car
my_car.drive_car()


#the car's condition should now be used
print my_car.condition

#5

Thank you so much. #comments really helped me to understand. :smiley:


#6

you need to call def display_car(self): to change the value of condition