【发布时间】:2015-07-05 08:05:23
【问题描述】:
我不断收到错误
NameError: name animal_type is not defined
或
TypeError: __init__() missing 2 required positional arguments: 'animal_type' and 'name'
无论我是否将行改为
animal = Animal.Animal(animal_type, animal)
或
animal = Animal.Animal()
以后会有这个:
#Add animal to list
def addAnimal(animal):
atype = input("What type of animal would you like to create? ")
theAnimal = atype
theAnimal = Animal.Animal()
theAnimal.set_animal_type(atype)
aname = input("What is the animal's name? ")
theName = aname
theName = Animal.Animal()
theName.set_name(aname)
zookeeper.add_animal(theAnimal, theName)
为了提供一些背景知识,这个程序有 2 个可以导入的类。
这是我正在调用的一个:
import random
class Animal:
#initialize attributes
def __init__(self,animal_type, name, mood = None):
self.__animal_type = animal_type
self.__name = name
if mood:
self.__mood = mood
else:
self.check_mood()
# set methods
def set_animal_type(self, animal_type):
self.__animal_type = animal_type
def set_name(self, name):
self.__name = name
【问题讨论】:
标签: python class python-3.x typeerror nameerror