[通过类中的字典调用的函数

问题描述 投票:0回答:1

在此代码块中,我使用的是名为class Cthulhu_room(Scene)的类。我的想法是使用字典来调用第二个函数attack(self),但是我不知道该怎么做。我希望您对此有所帮助,我也收到以下错误

错误*

追踪(最近通话):在第134行的文件“ mygame.py”中Cthulhu_room(Scene)类:在Cthulhu_room中的文件“ mygame.py”,第136行'攻击':Attack()NameError:未定义名称“攻击”

class Cthulhu_room(Scene):
    floor = {
        'attack': attack()
    }

    def enter(self):
            print(dedent("""Here you see the great evil Cthulhu
            He, it, whatever stares at you and you go insane.
            Do you flee for your life or eat your head"""))

            choice = input("> ")

            # this is the same as if choice == "flee"
            if "flee" in choice:
                return "laser_weapon_armory"
            elif "head" in choice:
                return "death"

            elif "danger" in choice:
                return Cthulhu_room.floor.get('attack')
            else:
                print("back to the beginning")
                return 'central_corridor'



    def attack(self):
        print("Kill this an intruder or die")

        choice = input("> ")
        if "kill" in choice:
            print("Welcome to the next level")
            return "laser_weapon_armory"
        else:
            return 'death'
python python-3.x
1个回答
-1
投票

您的攻击方法已绑定到类的对象。因此要调用它,您必须使用self。

这样定义您的字典。

floor = {
        'attack': self.attack()
    }
© www.soinside.com 2019 - 2024. All rights reserved.