# Buggy code
def open_swap_menu(self):
    """Open menus to swap monsters in party

    :return: None
    """

    def swap_it(menuitem):
        monster = menuitem.game_object
        trans = translator.translate
        if monster in self.game.get_state_name("CombatState").active_monsters:
            tools.open_dialog(
                self.game, [trans("combat_isactive", {"name": monster.name})]
            )
            return
        elif monster.current_hp < 1:
            tools.open_dialog(
                self.game, [trans("combat_fainted", {"name": monster.name})]
            )
        player = self.game.player1
        target = player.monsters[0]
        swap = Technique("Swap")
        swap.other = monster
        combat_state = self.game.get_state_name("CombatState")
        combat_state.enqueue_action(player, swap, target)
        self.game.pop_state()  # close technique menu
        self.game.pop_state()  # close the monster action menu

    menu = self.game.push_state("MonsterMenuState")
    menu.on_menu_selection = swap_it
    menu.anchor("bottom", self.rect.top)
    menu.anchor("right", self.game.screen.get_rect().right)
    menu.monster = self.monster


# Traceback (KeyError)
Traceback (most recent call last):
File "./tuxemon.py", line 60, in <module>
main()
File "/tmp/Tuxemon/tuxemon/core/main.py", line 109, in main
control.main()
File "/tmp/Tuxemon/tuxemon/core/control.py", line 512, in main
self.main_loop()
File "/tmp/Tuxemon/tuxemon/core/control.py", line 573, in main_loop
self.update(time_delta)
File "/tmp/Tuxemon/tuxemon/core/control.py", line 203, in update
state.update(dt)
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 156, in update
self.update_phase()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 328, in update_phase
self.handle_action_queue()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 338, in handle_action_queue
self.check_party_hp()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 614, in check_party_hp
self.faint_monster(monster)
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 581, in faint_monster
awarded_exp = monster.total_experience/monster.level/len(self._damage_map[monster])
KeyError: <core.components.monster.Monster object at 0x7fb24236ddd0>

# Fixed code
def open_swap_menu(self):
    """Open menus to swap monsters in party

    :return: None
    """

    def swap_it(menuitem):
        monster = menuitem.game_object
        trans = translator.translate
        if monster in self.game.get_state_name("CombatState").active_monsters:
            tools.open_dialog(
                self.game, [trans("combat_isactive", {"name": monster.name})]
            )
            return
        elif monster.current_hp < 1:
            tools.open_dialog(
                self.game, [trans("combat_fainted", {"name": monster.name})]
            )
        player = self.game.player1
        target = player.monsters[0]
        swap = Technique("technique_swap")
        swap.other = monster
        combat_state = self.game.get_state_name("CombatState")
        combat_state.enqueue_action(player, swap, target)
        self.game.pop_state()  # close technique menu
        self.game.pop_state()  # close the monster action menu

    menu = self.game.push_state("MonsterMenuState")
    menu.on_menu_selection = swap_it
    menu.anchor("bottom", self.rect.top)
    menu.anchor("right", self.game.screen.get_rect().right)
    menu.monster = self.monster