家庭作业中的Python属性错误

时间:2015-04-15 00:39:46

标签: python attributeerror

我正在运行以下代码,该代码直接从我的使用Python 本书开始复制。我一直收到以下错误:

AttributeError: 'Car' object has no attribute 'get_make'

代码:

import vehicles

def main():
    used_car = vehicles.Car('Audi', 2007, 12500, 21500.00, 4)

    print('Make: ', used_car.get_make())
    print('Model: ', used_car.get_model())
    print('Mileage: ', used_car.get_mileage())
    print('Price: ', used_car.get_price())
    print('Number of doors: ', used_car.get_doors())

main()

这是车辆模块:

class Automobile:

    def __init__(self, make, model, mileage, price):
        self.__make = make
        self.__model = model
        self.__mileage = mileage
        self.__price = price

        def set_make(self, make):
            self.__make = make

        def set_model(self, model):
            self.__model = model

        def set_mileage(self, mileage):
            self.__mileage = mileage

        def set_price(self, price):
            self.__price = price


        def get_make(self):
            return self.__make

        def get_model(self):
            return self.__model

        def get_mileage(self):
            return self.__mileage

        def get_price(self):
            return self.__price

class Car(Automobile):

    def __init__(self, make, model, mileage, price, doors):
        Automobile.__init__(self, make, model, mileage, price)
        self.__doors = doors

    def set_doors(self, doors):
       self.__doors = doors

    def get_doors(self):
        return self.__doors

1 个答案:

答案 0 :(得分:3)

您的缩进不正确。它应该是这样的:

class Automobile:

    def __init__(self, make, model, mileage, price):
        self.__make = make
        self.__model = model
        self.__mileage = mileage
        self.__price = price

    def set_make(self, make):
        self.__make = make

    def set_model(self, model):
        self.__model = model
    def set_mileage(self, mileage):
        self.__mileage = mileage

    def set_price(self, price):
        self.__price = price

    ....

目前,由于您的缩进,这些功能是在__init__方法中创建的,而不是作为方法附加到您的课程。