Modifying Member Classes


#1



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


#2

... and only the self keyword.


#3

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.

Cheers


#4

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


#5

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


#6

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'

#7

Yep! Changing mgp to self.mpg fixed this


#8

Aside:

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.


#9

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


#10

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