As there's no mention of the value, I set the base damage to 50 in each scenario.
def compare_attack_types(first_type, second_type): if first_type == "fire": if second_type == "grass": return 2 if second_type == "water": return 0.5 if first_type == "grass": if second_type == "fire": return 0.5 if second_type == "water": return 2 if first_type == "electric": if second_type == "water": return 2 if first_type == "water": if second_type == "fire": return 2 if second_type == "grass": return 0.5 return 1 def calculate_damage(base_damage, own_type, opponent_type, attack, defense): return base_damage * (attack / defense) * compare_attack_types(own_type, opponent_type) print(calculate_damage(50, "grass", "electric", 57, 19)) print(calculate_damage(50, "grass", "water", 40, 40)) print(calculate_damage(50, "grass", "fire", 35, 5)) print(calculate_damage(50, "fire", "electric", 10, 2))
150 100 175 250
Ok cool. I'll use 50, too.
Here'a another Python implementation:
SUPER_EFFECTIVE = 'super_effective' NEUTRAL = 'neutral' NOT_VERY_EFFECTIVE = 'not_very_effective' EFFECTIVENESS_MULTIPLIER = { SUPER_EFFECTIVE: 2, NEUTRAL: 1, NOT_VERY_EFFECTIVE: .5} FIRE = 'fire' GRASS = 'grass' WATER = 'water' ELECTRIC = 'electric' ATTACK_ON_DEFENSE_EFFECTIVENESS = { FIRE: { FIRE: NOT_VERY_EFFECTIVE, GRASS: SUPER_EFFECTIVE, WATER: NOT_VERY_EFFECTIVE, ELECTRIC: NEUTRAL, }, GRASS: { GRASS: NOT_VERY_EFFECTIVE, WATER: SUPER_EFFECTIVE, ELECTRIC: NEUTRAL, FIRE: NOT_VERY_EFFECTIVE }, WATER: { WATER: NOT_VERY_EFFECTIVE, GRASS: NOT_VERY_EFFECTIVE, ELECTRIC: NOT_VERY_EFFECTIVE, FIRE: SUPER_EFFECTIVE }, ELECTRIC: { ELECTRIC: NOT_VERY_EFFECTIVE, GRASS: NEUTRAL, WATER: SUPER_EFFECTIVE, FIRE: SUPER_EFFECTIVE } } def _calculate_effectiveness(attack_type, defense_type): effectiveness_name = ATTACK_ON_DEFENSE_EFFECTIVENESS[attack_type][defense_type] return EFFECTIVENESS_MULTIPLIER[effectiveness_name] def _total_damage(base_damage, attack, defense, effectiveness): return base_damage * (attack / defense) * effectiveness def calculate_damage(base_damage, attack_type, defense_type, attack_power, defense_power): return _total_damage( base_damage, attack_power, defense_power, _calculate_effectiveness(attack_type, defense_type) ) print(calculate_damage(50, "grass", "electric", 57, 19)) print(calculate_damage(50, "grass", "water", 40, 40)) print(calculate_damage(50, "grass", "fire", 35, 5)) print(calculate_damage(50, "fire", "electric", 10, 2))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
As there's no mention of the value, I set the base damage to 50 in each scenario.
Code
Results
150
100
175
250
Ok cool. I'll use 50, too.
Here'a another Python implementation: