为什么模块不是callab

2024-04-20 05:43:43 发布

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

我已经完成了我的大部分程序,但我不断得到的错误,似乎无法找出为什么它一直这样做。我也试过animal_list = Zoo.Zoo()

line 43, in addAnimal
    animal_list = Zoo()
TypeError: 'module' object is not callable

这是我的一些程序

import Animal
import Zoo
def main():
    #set user choice 
    choice = 0
    while choice != "3":
        display_menu()

        #get user's choice
        choice = str(input("What would you like to do? "))

        #Perform selected choice
        if choice.isalpha():
            print("Please enter a numeical value")

        elif choice == "1":
            addAnimal()

以及

#Add animal to list
def addAnimal():
    atype = input("What type of animal would you like to create? ")
    aname = input("What is the animal's name? ")
    theAnimal = Animal.Animal(atype, aname)
    theAnimal.set_animal_type(atype)
    theAnimal.set_name(aname)
    animal_list = Zoo()
    animal_list.add_animal(theAnimal,Animal)

Tags: to程序inputiswhatlistsetchoice
1条回答
网友
1楼 · 发布于 2024-04-20 05:43:43

从你的其他问题来看,你的Zoo课是完全错误的。你知道吗

你的Zoo类应该这样写:

class Zoo:

    def __init__(self):
        self.__animals = []

    def add_animal(self, animals):
        self.__animals.append(animal)

    def show_animals(self):
        size = len(self.__animals)
        if size == 0:
            print("There are no animals in your zoo!")
        else:
            return __animals

而是定义如下方法:

def __init__(Animal):

定义变量如下:

Animal.__animals = []

这根本说不通。你知道吗

您的问题是使用了模块(Animal)而不是self。我不知道你是从哪里得到这个想法的,但你可能想细读一下class definition in the Python documentation。你知道吗

相关问题 更多 >