Can someone please tell me what's wrong here? 20. Classes - 10/11


#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):
return "This is a %s %s with %d MPG." % (self.color, self.model, self.mpg)
def drive_car(self):
self.condition = "used"

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

print my_car.condition

my_car.drive_car()

print my_car.condition

class ElectricCar(Car):
def init(self, model, color, mpg, battery_type):
Car.init(self, model, color, mpg)
self.battery_type = battery_type
def drive_car():
self.condition = "like new"

my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")

print my_car.condition

my_car.drive_car()

print my_car.condition


#2
class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type
    def drive_car(self):
        Car.condition = "like new"
my_car = ElectricCar("lotus", "brown", 4, "molten salt")
print ElectricCar.condition
my_car.drive_car()
print ElectricCar.condition

#3

You forgot the 'self' parameter in the drive_car method. Python methods need the self parameter because Python is designed to be a very explicit language.

Also if you use </> button you can format your code inside your question :smile:


#4

Hey this is also the possible solution:

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 "+ self.color + " "+ self.model + " with " + str(self.mpg) + " MPG."

def drive_car(self):
    self.condition = 'used'

class ElectricCar(Car):

def __init__(self,battery_type,model,color,mpg):
    self.battery_type = battery_type
    self.model = model
    self.color = color
    self.mpg   = mpg

def drive_car(self):
    Car.condition = "like new"

my_car = ElectricCar("molten salt","Grey","Honda",98)

print my_car.condition
my_car.drive_car()
print my_car.condition
print my_car.display_car()


#5

Thank you so much guys :smile:


#6

I copy pasted yours, and somehow it still won't change the condition. What am I doing wrong here?

class ElectricCar(Car):
   def __init__(self,battery_type,model,color,mpg):
    self.battery_type = battery_type
    self.model = model
    self.color = color
    self.mpg   = mpg

    def drive_car(self):
        Car.condition = "like new"
    
my_car = ElectricCar("molten salt","Grey","Honda",98)

print my_car.condition
my_car.drive_car()
print my_car.condition
print my_car.display_car()

#7

I think you should check your level (tab) of your code in editor.
In your code show that level of drive_car() method is under init() method.

btw, is there anyone can explain why I have same result when my code in drive_car() method (of ElectricCar class) in two ways:
1. Car.condition = 'like new'
2. self.condition = 'like new'
It made me confused.
Thanks.


#8

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

class ElectricCar(Car):
def init(self, model, color, mpg, battery_type):
self.model=model
self.color=color
self.mpg=mpg
self.battery_type=battery_type

my_car=ElectricCar("Ford", "black", 20, "molten salt")
class ElectricCar(Car):
def init(self, model, color, mpg, battery_type):
Car.init(self, model, color, mpg)
self.battery_type = battery_type
def drive_car(self):
Car.condition = "like new"
my_car = ElectricCar("lotus", "brown", 4, "molten salt")
print ElectricCar.condition
my_car.drive_car()
print ElectricCar.condition

this worked for me


#9

needs to be my_car.condition = "like new"