我的代码需要帮助我想在display_details中显示句子

2024-03-28 22:07:31 发布

您现在位置:Python中文网/ 问答频道 /正文

    class Pet:
        def __init__(self, name, animal_type, age):
            self.__name = name
            self.__animal_type = animal_type
            self.__age = age

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

        def set_animal_type(self, animal_type):
            self.__animal_type = animal_type

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

        def get_name(self):
            return self.__name

        def get_animal_type(self):
            return self.__animal_type

        def get_age(self):
            return self.__age

        def display_details(self):
            return "Pet {} is a {}, and it is {} years old".format(self.__name, self.__animal_type, 
self.__age)

//问题就在这里^

name = input("Enter pet name:")
    animal_type = input("Enter pet type:")
    age = int(input("Enter age of pet:"))
    Pet(name, animal_type, age)

//我试图在display_details下显示句子,但在输出中看不到它 //llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll


Tags: nameselfinputagegetreturndeftype
1条回答
网友
1楼 · 发布于 2024-03-28 22:07:31

您是否创建了宠物类的实例?即:

name = input("Enter pet name:")
animal_type = input("Enter pet type:")
age = int(input("Enter age of pet:"))

my_pet = Pet(name, animal_type, age)
print(my_pet.display_details())

相关问题 更多 >