gpt4 book ai didi

python - 内置.AttributeError : 'Pet' object has no attribute 'get_name'

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:18 26 4
gpt4 key购买 nike

我目前收到此错误:

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 - 内置.AttributeError : 'Pet' object has no attribute 'get_name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com