if-else块的输出不正确

时间:2018-01-16 15:09:33

标签: python python-3.x

这是父类

class Car():
def __init__(self, company_name, year, model_name):
    self.company_name = company_name
    self.year = year
    self.model_name = model_name
    self.odometer_reading = 0

def display_info(self):
    print(self.company_name.title() + ' ' + self.model_name.title() + ' ' + str(self.year))

def change_reading(self, odometer_reading):
    self.odometer_reading += odometer_reading

def read_reading(self):
    print("Miles: " + str(self.odometer_reading))

这是儿童班

class ElectricCar(Car):
def __init__(self, company_name, year, model_name):
    super().__init__(company_name, year, model_name)

def battery_size(self, model):
    if (model == 'e'):
        self.model_name = model
        self.battery = 70
    elif (model == 'x'):
        self.model_name = model
        self.battery = 90
    else:
        self.model_name = model
        self.battery_size = 80
    print("Model " + self.model_name.title() + " has " + str(self.battery) + "kWh battery")

创建类的实例并调用它后,我能够将输出作为reqd

ecar1 = ElectricCar('tesla', 2017, 'e')
ecar1.battery_size('e')
ecar1.battery_size('x')

但对于给定的陈述,我无法这样做。为什么?

ecar1.battery_size('faraday')

1 个答案:

答案 0 :(得分:1)

在你的else语句中,你有

self.battery_size = 80

应该是

self.battery = 80