Python宠物类

时间:2017-04-30 01:09:48

标签: python

我有class Pet()main()功能,如下所示。我收到语法错误,但不清楚原因。

class Pet(object):
    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_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 main():

        name = input('What is the name of the pet: ')
        animal_type = input('What type of pet is it: ')
        age = int(input('How old is your pet: ')
        pets = Pet(name, animal_type, age)

        print('This will be added to the records.')
        print('Here is the data you entered: ')
        print('Pet Name: ', pets.get_name())
        print('Animal Type: ', pets.get_animal_type())
        print('Age: ', pets.get_age())

main()

我一直收到宠物行的语法错误

1 个答案:

答案 0 :(得分:0)

您的代码中有2个错误,首先,您的init方法需要使用双下划线编写,例如prorate,其次,您在年龄输入行上缺少__init__。如果你把这样的代码放在一起,它应该可以工作。

)