Python eTypeError:uuu init_uuuu()缺少1个必需的位置参数:';类型';
我有两个节目。 第一个是animalGenerator.py:Python eTypeError:uuu init_uuuu()缺少1个必需的位置参数:';类型';,python,class,Python,Class,我有两个节目。 第一个是animalGenerator.py: # This program generates a name and an animal type import Animals print("Welcome to the animal generator!") print("This program creates Animal objects") def main(): animals = make_list() print("\nAnimal L
# This program generates a name and an animal type
import Animals
print("Welcome to the animal generator!")
print("This program creates Animal objects")
def main():
animals = make_list()
print("\nAnimal List")
print("-------------")
display_list(animals)
def make_list():
animal_list = []
again = 'y'
while again.lower() == 'y':
typ = input("\nWhat type of animal would you like to create? ")
nam = input("What is the animal's name? ")
ani = Animals.Animal(typ, nam)
animal_list.append(ani)
again = input("\nWould you like to add more animals (y/n)? ")
return animal_list
def display_list(animal_list):
for item in animal_list:
print("\n" + item.get_name() + "the" + item.get_Type() + "is" + item.get_mood())
main()
第二个程序Animals.py:
import random
class Animal:
def __init__(self, name, mood, Type):
self.__name = name
self.__mood = mood
self.__animal_type = Type
def set_name(self, name):
self.__name = name
def set_mood(self, mood):
self.__mood = mood
def set_animal_type(self, Type):
self.__animal_type = Type
def toss(self):
if random.randint(1, 2, 3) == 1:
self.__mood = 'Happy'
elif random.randint(1, 2, 3) == 2:
self.__mood = 'Hungry'
elif random.randint(1, 2, 3) == 3:
self.__mood = 'Sleepy'
def get_name(self):
return self.__name
def get_mood(self):
return self.__mood
def get_animal_type(self):
return self.__animal_type
运行第一个程序时,我收到一个错误(标题)
这是回溯:
Traceback (most recent call last):
File "C:\Users\Dahlia\Desktop\McCuneShannonAnimals\animalGenerator.py", line 42, in <module>
main()
File "C:\Users\Dahlia\Desktop\McCuneShannonAnimals\animalGenerator.py", line 11, in main
animals = make_list()
File "C:\Users\Dahlia\Desktop\McCuneShannonAnimals\animalGenerator.py", line 30, in make_list
ani = Animals.Animal(typ, nam)
TypeError: __init__() missing 1 required positional argument: 'Type'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Dahlia\Desktop\mccunshannonanimals\animalGenerator.py”,第42行,在
main()
文件“C:\Users\Dahlia\Desktop\mccunshannonanimals\animalGenerator.py”,第11行,主目录
动物=制造清单()
文件“C:\Users\Dahlia\Desktop\mccunshannonanimals\animalGenerator.py”,第30行,在制作列表中
ani=动物。动物(典型,nam)
TypeError:\uuuu init\uuuu()缺少1个必需的位置参数:“Type”
有人能帮我弄清楚为什么“Type”是一个缺少的位置参数吗 第二个程序看起来不像是有效的python,缺少了一定程度的缩进,所以我很惊讶它居然能运行
然而,对于最初的问题-您正在使用
(typ,nam)
实例化动物,但是在构造函数中它还需要第三个参数-类型
-(名称、情绪、类型)
-这是解释器抱怨的位置参数。您定义了动物
的\uuuuu init()
asdef\uuu init\uuu(自我、姓名、情绪、类型)
。但是,您可以用ani=anies.Animal(typ,nam)
来调用它——这是预期的三个参数中的两个。确保每个方法都定义正确,并且始终根据它们的定义正确调用方法。错误消息告诉您错误:您试图调用初始值设定项(_uinit__()),参数数量不足。问题在于ani=anies.Animal(typ,nam)
其中仅给出两个参数,而预期为三个。type
与type
不同。不,type
在Python中不是关键字<代码>类型()是一个内置函数,不过。@PadraicCunningham你比我快了四秒钟<不过,从技术上讲,code>type也不是一个关键词。您可以将其用作变量;这只是坏习惯。