1

In this block of code I'm using a class called class Cthulhu_room(Scene). My idea is to use a dictionary to call the second function attack(self) but I don't know how to go about this. I would like your help to this, I also get the following errors

ERRORS*

Traceback (most recent call last): File "mygame.py", line 134, in class Cthulhu_room(Scene): File "mygame.py", line 136, in Cthulhu_room 'attack': attack() NameError: name 'attack' is not defined

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'
  • Does this answer your question? [Using a dictionary to select function to execute](https://stackoverflow.com/questions/9168340/using-a-dictionary-to-select-function-to-execute) – pfabri May 06 '20 at 13:44
  • 1
    @pfabri There are additional problems OP is facing due to the dict being a class attribute. Those other answers don't address those problems. – Tom Karzes May 06 '20 at 13:46
  • @TomKarzes how do I address this problem ? I'm relatively new to programming, so I'm designing this program to understand classes and objects. This is just one aspect of the entire code block which I written so far. I would like to know how to call the attack function within this class. –  May 07 '20 at 01:40
  • @TomKarzes The easier route would be to return **Cthulhu_room.attack(self)**, this will easily call the function **attack(self)**. But I would like to know how to use this dictionary to call this within this class itself. –  May 07 '20 at 01:50
  • 1
    @DanielIhenacho You can create a usable dictionary by moving it to after the method definitions within the class, and remove the parentheses from the method name so that it doesn't invoke it but merely saves an unbound reference to it. You can then invoke it as `self.floor['attack'](self)`. – Tom Karzes May 07 '20 at 02:00
  • @TomKarzes Your advice worked. Thanks a lot, knowing this way of calling a function from a dictionary is really helpful within a class is really helpful. –  May 07 '20 at 02:23
  • there really is no need for this dictionary, *you can just use the class*, so just use `return self.attack()` – juanpa.arrivillaga May 07 '20 at 03:45
  • 1
    @juanpa.arrivillaga Yeah, the dictionary only makes sense if there's a need to look up methods based on a string name, which the posted code doesn't seem to be doing. Even if that is needed, if the strings match the method names, it could use `self.__class__.__dict__['attack'](self)` to do it, without the need to create a dict. – Tom Karzes May 07 '20 at 06:57
  • 1
    @TomKarzes yep, or just `getatter(self, 'attack')()` to invoke the descriptor protocol for instance-binding. – juanpa.arrivillaga May 07 '20 at 07:02
  • @juanpa.arrivillaga Yes, that's better. – Tom Karzes May 07 '20 at 07:48

0 Answers0