Bernard Allotey
Bernard Allotey

Reputation: 822

What is the best way to determine a value based on two different values in Python

I need to determine the amount of damage done to a character depending on both the enemy and the weapon. Each weapon does a different amount of damage depending on who it attacks. What would be the best way to do this without writing a lot of if statements?

Upvotes: 1

Views: 47

Answers (1)

Jayson Chacko
Jayson Chacko

Reputation: 2418

If you are looking at a generic approach, I would recommend building a lookup dictionary such as the following. You can use this dictionary to compute the damage,

def get_damage(enemy_type,weapon):
    damage_dict = {"enemy_type1":{'weapon1':10,'weapon2':20,'weapon3':50},
                   "enemy_type2":{'weapon1':5,'weapon2':20,'weapon3':45},
                   "enemy_type3":{'weapon1':15,'weapon2':20,'weapon3':40,'weapon4':50},
                   }
    return damage_dict.get(enemy_type).get(weapon)

print(get_damage('enemy_type2','weapon2'))

Upvotes: 1

Related Questions