简体   繁体   中英

Identifying a key in one dict, and using it to modify values of another dict

For a text-based RPG I'm creating, I have dictionaries outlining various in-game religions, races, etc. Some values of these religions include buffs for player's stats, eg:

religion_Dict = {'Way of the White': {'buffs': {'intelligence': 5, 'defense': 3},
                                     'abilities': [...],
                                     'description': '...'}}

My question comes along when trying to apply a religion's stat buffs to a player's stats. If I have a player class that looks something like:

class Player(object)    
    def __init__(self):
        ...
        self.religion = None
        self.stats = {'intelligence': 10, 'defense': 8}

Now, let's say the player joins the religion Way of the White , how do I go about identifying the keys intelligence and defense and their respective values -- inside of the dictionary religion_dict -- and apply them onto the values of the player's stats dictionary?

I know I can pull the key names with religion_Dict.keys() or a basic for loop, but how can I use that to correctly modify the corresponding player stat values?

I'm sure I'm just missing a basic concept. In any case, thanks to anyone willing to help answer this simple question! I appreciate it!

Here is a sketch of how you would go about this:

religion_Dict = {'Way of the White': {'buffs': {'intelligence': 5, 'defense': 3},
                                     'abilities': [...],
                                     'description': '...'}}
buffs = religion_Dict['Way of the White']['buffs']

for key in buffs:
    player.stats[key] = player.stats.get(key,0) + buffs[key]

Of course, you should wrap this logic in a method in your Player class, but the logic above is what you are looking for. Notice the .get method takes a second argument which is a default value it returns if there is no key value. Thus, this line will add 1 to whatever stat is there, and if it doesn't exist, it adds 1 to 0.

This adds a method to Player that assigns the values from your dictionary to player stats. It uses get to ensure that the value is in the dictionary and that it contains the field buffs . If so, it gets the values for intelligence and defense and adds them to the player's stats.

class Player(object)    
    def __init__(self):
        ...
        self.religion = None
        self.stats = {'intelligence': 10, 'defense': 8}

    def join_religion(religion):
        stats = religion_dict.get(religion)
        if stats and 'buffs' in stats:
            self.intelligence += stats['buffs'].get('intelligence', 0)
            self.defense += stats['buffs'].get('defense', 0)

p = Player()
p.join_religion('Way of the White')
self.stats = religion_Dict['Way of the White']['buffs']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM