我创建了一个敌方类,它从文件中读取数据以获取其实例变量。 针对不同类型的敌人,文件被写入不同的数组。他们工作得很好。当我把它们写到对象然后尝试打印它们时,我得到了一个错误:
AttributeError: 'NoneType' object has no attribute 'type'
如果我不打印'type'属性,那么它会显示:
^{pr2}$课程是:
def Enemy(object):
def __init__(self, TypeStats):
self.type = TypeStats[0][0]
self.health = int(TypeStats[0][1])
self.strength = int(TypeStats[0][2])
self.dead = False
将数组写入对象并打印变量的代码是:
Bandit = Enemy(BanditStats)
print Bandit.type, Bandit.health, Bandit.strength
我将其作为二维数组写入的原因是,当我将数据从文件写入数组时,它会创建一个数组:
BanditStats = [['Bandit', '80','7']]
我不知道为什么会这样,很容易解决吗?在
不能使用
def
来定义类。它应该是class
:相关问题 更多 >
编程相关推荐