宠物类python

时间:2014-09-25 05:59:54

标签: python

此作业的说明: 编写一个名为Pet的类,它应具有以下属性:

  • __名称
  • __ animal_type
  • __年龄

然后会有以下方法:

  • set_name
  • set_animal_type
  • set_age
  • GET_NAME
  • get_animal_type
  • get_age

编写完此类后,编写一个程序,创建该类的对象,并提示用户输入宠物的名称,类型和年龄。数据应存储为对象的属性。使用对象的访问器方法检索宠物的名称,类型和年龄并在屏幕上显示。

这是我的代码:

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


# The main function
def main():
    # Create an object from the Pet class
    pets = Pet(name, animal_type, age)

    # Prompt user to enter name, type, and age of pet
    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: '))
    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()

当我运行程序时,它给了我这个错误: UnboundLocalError:局部变量' name'在分配前引用

3 个答案:

答案 0 :(得分:1)

在main()函数中,Pet()类对象需要参数。因此,您首先需要提供参数,然后将其输入到Pet()类对象中。

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_age(self):
        return self.__age

    def get_animal_tpe(self):
        return self.__animal_type


def main():
    pet_name = input('Please enter your pet\'\s name: ')
    pet_type = input('What animal is your pet?')
    pet_age = float(input('What is the age of your pet?'))

    pet_specs = Pet(pet_name,pet_type,pet_age)

    print('pet name is ', pet_specs.get_name())
    print('pet type is ', pet_specs.get_animal_tpe())
    print('pet age is ', pet_specs.get_age())

main()

答案 1 :(得分:0)

此行包含未定义的变量 -

pets = Pet(name, animal_type, age)
           ^^^^^^^^^^^^^^^^^^^^^^

此行应在您收到所有输入后。

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: '))
#The line is moved here -
pets = Pet(name, animal_type, age)

答案 2 :(得分:0)

问题是你在向用户询问宠物的详细信息之前正在创建宠物,在完成输入后需要移动“创建宠物”行:

# The main function
def main():
    # Create an object from the Pet class
    # pets = Pet(name, animal_type, age) --------------------- move this
                                                               #  |
    # Prompt user to enter name, type, and age of pet          #  |
    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) # <---------------------- here
    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()