Modifying Member Classes


Error: Oops, try again. Make sure you pass the self keyword to the drive_car() method.
I can't figure this out. Any help is appreciated! Here's my code:

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


... and only the self keyword.


Great, thank you! This worked when I changed def drive_car(self, condition) to def drive_car(self) . Just in case anyone else out there is still wondering.



There's another problem: I think you may need to use "str(self.mpg)" instead of "mpg" in "display_car"function.


Yes, I had changed that without noting it here. But, thank you!


No doubt you raised this exception, that alerted you...

Traceback (most recent call last):
  File "<pyshell#165>", line 1, in <module>
    print ("%s") % 5
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'


Yep! Changing mgp to self.mpg fixed this



No doubt the author was embarrassed by the typo that made it into production and is no longer reparable. 'MPG' was meant to be 'MPH', as in the film, Back to the Future. Had this mishap never occured, the reference would not be lost on so many learners. I feel for the author.


Actually had wondered about this when I did this lesson ha. 88 mpg is unusually high, especially for a DeLorean! MPH makes a lot more sense


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