python - 内置属性错误: 'Pet' object has no attribute 'get_name'

标签 python python-3.3

我目前收到此错误:

builtins.AttributeError: 'Pet' object has no attribute 'get_name'

它显示的是程序末尾的 for 循环之后的代码行 print(item.get_name())

这是我的类(class):

class Pet:

    def __init__(self, name, type, age):
        self.__name = name
        self.__type = type
        self.__age = age

        def set_name(self, name):
            self.__name = name

        def set_type(self, type):
            self.__type = type

        def set_age(self, age):
            self.__age = age

        def get_name(self):
            return self.__name

        def get_type(self):
            return self.__type

        def get_age(self):
            return self.__age

这是我的程序:

import pet

def main():

    animals = make_list()

    print('Here is the data you entered:')
    display_list(animals)

def make_list():

    animal_list = []

    print('Enter data for 3 animals.')
    for count in range(1,4):
        print('Pet number '+str(count) + ':')
        name = input('Enter the name: ')
        type = input('Enter the type: ')
        age = input('Enter the age: ')
        print()

        animal = pet.Pet(name, type, age)

        animal_list.append(animal)

    return animal_list

def display_list(animal_list):

    for item in animal_list:
        print(item.get_name())
        print(item.get_type())
        print(item.get_age())
        print()

main()

最佳答案

所有Pet 的方法都需要以相同的缩进级别开始。您需要将从 set_nameget_age 的每一行取消缩进一级。

关于python - 内置属性错误: 'Pet' object has no attribute 'get_name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136630/

相关文章:

python Pandas : Count of datetime items between dateindex and next dateindex

python - 如何使用置换数组有效地置换稀疏(Numpy)矩阵中的行?

python - 使用seaborn时条件和输入之间的形状不一致

python - 安装 easy_install... 以安装 lxml

Python 日志模块不遵守低于 '30' 的 setLevel(警告)?

python - 如何根据另一个列表对列表进行排序? Python

python - 在列表中移动项目?

python - 来自 URL 的 Blender Material

python - 在 Python 3.3 venv (OS X/Homebrew) 中安装 distribute

python - 在没有嵌套循环的情况下访问字典的嵌套级别