AttributeError:type object' '没有属性' '

时间:2017-04-18 18:33:01

标签: python python-3.x class attributeerror

我已在网站上询问过类似的内容,但我无法克服此错误。我使用的代码是:

class Enemy:
# Base class to create all future enemies
def __init__(self, name, hp, dmg, desc):
    self.name = name  # Enemy name
    self.hp = hp  # Enemy hp
    self.dmg = dmg  # Enemy damage
    self.desc = desc  # Enemy description (Will make sense later)

def is_alive(self):
    return self.hp > 0

上面的代码用于定义所有未来的敌人,正如我在#上所述。然后,我创建了三个敌人进行测试,因为我的游戏会从列表中产生随机遭遇。但是,我只会列出一个,因为错误发生在任何敌人身上:

class Goblin(Enemy):
def __init__(self):
    super().__init__(name = "Goblin",
                     hp = 40,
                     dmg = 33,
                     desc = "An ordinary goblin runs in your direction!")

然后,正如敌人所做的那样,地牢的房间也有一个模板类和一个随机目的列表,但正如我之前所做的那样,我只会列出一个,因为它是导致错误的一个对我来说:

class Room:
# Template to define all future rooms
    def __init__(self, intro):
        self.intro = intro

class VsRoom(Room):
# Room with an enemy
    duelist = None

    def __init__(self):
        self.duelist = duelist = random.choice(Enemy_List)
        super().__init__(intro = "There's an enemy here! \n%s" %(duelist.desc))

然后是" def"运行游戏(目前仅用于测试):

def print_room():
#  Tells the player the type of room they are
    print("You enter the next room...")
    print()
    gen_room = random.choice(Rooms)()  # ("Rooms" is a list) 
    print(gen_room.intro)
    if gen_room == VsRoom:
        combat()  # No need to list the combat() definition, I think.

但是当我跑步时,我明白了:

AttributeError: type object 'Goblin' has no attribute 'desc'

我知道AttributeError在这里很容易找到,但似乎现有的问题都不适合我。如果有人知道代码失败的原因,请告诉我。

0 个答案:

没有答案