首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:“ElectricCar”对象没有特性“”battery_size“”

AttributeError:“ElectricCar”对象没有特性“”battery_size“”
EN

Stack Overflow用户
提问于 2020-06-30 02:20:43
回答 1查看 92关注 0票数 0

我正在学习"Python速成教程手册“这本书。我已经完全按照书中的方式编写了代码,但仍然出错。你能告诉我下面的代码有什么问题吗:

我得到了'AttributeError:'ElectricCar‘对象没有’battery_size‘属性。

班车:

代码语言:javascript
复制
def __init__(self, make, model, year):
    self.make=make
    self.model=model
    self.year=year
    self.odometer_reading = 0

def get_discriptive_name(self):
    long_name= f"{self.year}  {self.make}  {self.model}"
    return long_name.title()

def read_odometer(self):
    """Print a statement showing the car's mileage."""
    print(f"This car has {self.odometer_reading} miles on it.")

def update_odometer(self, mileage):
    """ 
    Set the Odometer reading to the given value.
    Reject the change if it attempts to roll the odometer back.
    """
    if mileage >= self.odometer_reading:
        self.odometer_reading = mileage
    else:
        print("You can't roll back an odometer!")

def increment_odometer(self,miles):
    """Add the given amount to the odometer reading."""
    self.odometer_reading += miles

"""Inheritance from parent/superclass to child/subclass."""

class ElectricCar(汽车):“表示汽车的各个方面,特定于电动汽车。”

代码语言:javascript
复制
def __intit__(self,make,model,year):

    super().__init__(make,model,year)
    
    self.battery_size = 75

def describe_battery(self):

    """Print a statement describing the battery size."""

    print(f"This car has a {self.battery_size}-kWh battery.")

my_tesla =ElectricCar(‘特斯拉’,‘型号s',2009)

print(my_tesla.get_discriptive_name())

my_tesla.describe_battery()

EN

回答 1

Stack Overflow用户

发布于 2020-06-30 02:26:49

代码语言:javascript
复制
class ElectricCar(Car):
   def __intit__(self,make,model,year):
      super().__init__(make,model,year)
      self.battery_size = 75

   def describe_battery(self):

       """Print a statement describing the battery size."""

       print(f"This car has a {self.battery_size}-kWh battery.")

如果您查看一下,就会发现init函数拼写错误

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62644393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档