在python

时间:2016-12-10 20:26:54

标签: python

以下是该计划的代码:

import petspgm

def main():
    pets = make_list()
    print('Here is the data you entered: ')
    display_lists(pets)

def make_list():
    pet_list = []

print('Enter the data for the 3 pets')
for count in range(1,4):
    # get the pet data
    print('Pet number' + str(count) + ':')
    pet_name = input("Enter your pet's name: ")
    pet_type = input("Enter the type of pet: ")
    pet_age = input("Enter your pet's age: ")
    print

    pet = petspgm.PetData(pet_name, pet_type, pet_age)

    pet_list.append(pet)

return pet_list

def display_lists(pet_list):
    for item in pet_list:
        print("Pet's name is: " + item.get_pet_name())
        print("Pet's type is: " + item.get_pet_type())
        print("Pet's age is: " + str (item.get_pet_age))
main()

以下是petspgm.py PetData类的代码:

class PetData:

# The __init__ method initializes the attributes
def __init__(self, pet_name, pet_type, pet_age):
    self.__pet_name = pet_name
    self.__pet_type = pet_type
    self.__pet_age = pet_age

# This method accepts an argument for the pet's name    
def set_pet_name(self, pet_name):
    self.__pet_name = pet_name

# This method acepts an argument for the pet's type
def set_pet_type(self, pet_type):
    self.__pet_type = pet_type

# This method accepts an argument for the pet's age
def set_pet_age(self, pet_age):
    self.__pet_age = pet_age

# This method returns the pet's name
def get_pet_name(self):
    return self.__pet_name

# This method returns the pet's type
def get_pet_type(self):
    return self.__pet_type

# This method returns the pet's age
def get_pet_age(self):
    return self.__pet_age

这是我得到的错误,它没有返回年龄..请帮助:

宠物的名字是:sam

宠物的类型是:狗

宠物的年龄是: petpg.PetData对象的绑定方法PetData.get_pet_age位于0x02D1E6D0

宠物的名字是: 兰特

宠物的类型是: 狗

宠物的年龄是: petpg.PetData对象的绑定方法PetData.get_pet_age位于0x02D1E730

宠物的名字是: 洒

宠物的类型是: 猫

宠物的年龄是: PetsDatam.PetData对象的绑定方法PetData.get_pet_age位于0x02D1E770

1 个答案:

答案 0 :(得分:1)

您错过了git --help来实际调用方法

()

应该是

print("Pet's age is: " + str (item.get_pet_age))