Modifying Member Variables


#1


https://codecademy.com/courses/python-intermediate-en-egNXj/1/2?curriculum_id=4f89dab3d788890003000096

I don't really understand why my code is throwing this error:

Traceback (most recent call last):
File "python", line 21, in
NameError: name 'drive_car' is not defined

When I have defined this method.


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))
        
    def drive_car(self):
        
        self.condition = "used"

my_car = Car("DeLorean", "silver", 88)

print my_car.condition

drive_car()

print my_car.condition


#2

If you had created a function by that name at the top level of the file, then you could do that. But your method resides inside a class, there is no name at the top level that refers directly to it.


#3

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