gpt4 book ai didi

python - 为什么模块不可调用

转载 作者:行者123 更新时间:2023-12-01 04:44:02 25 4
gpt4 key购买 nike

我已经完成了大部分程序,但是我不断收到错误,并且似乎无法弄清楚为什么它一直这样做。我也尝试过 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)

最佳答案

查看您的其他问题来看,您的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 .

关于python - 为什么模块不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29887256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com