8. Modifying member variables


#1




Oops, try again. You should call the drive_car() method of my_car to change the condition of my_car.


There is a subtle syntax error that I can't get passed. This code gets me the correct result in the console (as seen below), but I get the codecademy error message. I also tinkered a couple of times on the line that begins "Car.", but to no avail. When I remove "Car." and tinker with it, it says "drive_car" is not defined. Thanks in advance.


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

my_car = Car("DeLorean", "silver", 88)
print my_car.condition
Car.drive_car(my_car)
print my_car.condition

#Prints:
#new
#used
#None


#2

here:

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

you create an instance my_car of class. if you then want to call a method you simply do:

instance.method()

hope this helps


#3

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
Car.drive_car(my_car)
print my_car.condition

I still can't get it, please help

Oops, try again. You should call the drive_car() method of my_car to change the condition of my_car.


#4

uhm... is there something you don't understand about my explanation? it perfectly explains what is wrong with this line:

Car.drive_car(my_car)

If so, please tell me what is unclear about my explanation.


#5

My problem is that I don't see anything wrong with that line of code.


#6

methods should be called on instance(s), not on the class itself.


#7

Thanks i got it

I used the class


#8

yea, please now read the original reply i made to this topic.


#9

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