I'm a beginner practicing python by making a pokemon battle simulator, and I'm in the process of creating a damage modifier function.
The idea is:
If the pokemon's "attack type" is found in "attack_type_2x_dmglist", the pokemon deals twice the damage. "Very effective"
If the pokemon's "attack type" is found in "attack_type_half_dmglist", the pokemon deals only half the damage. "Isn't very effective"
I've used print to make it easy to debug for now.
To verify this, I've done conditionals for type vs type.
normal_2x_dmglist = []
normal_half_dmglist = ["Rock","Dragon"]
rock_2x_dmglist = ["Fire","Ice","Fly","Bug",]
rock_half_dmglist = ["Fighting","Ground","Steel"]
water_2x_dmglist = ["Fire","Ground","Rock"]
water_half_dmglist = ["Water","Grass","Dragon"]
grass_2x_dmglist = ["Water","Ground","Rock"]
grass_half_dmglist = ["Fire","Grass","Poison","Fly","Bug","Dragon","Steel"]
fighting_2x_dmglist = ["Normal","Ice","Rock","Dark","Steel"]
fighting_half_dmglist = ["Poison","Fly","Psychic","Bug","Fairy"]
ground_2x_dmglist = ["Fire","Electric","Poison","Rock","Steel"]
ground_half_dmglist = ["Grass","Bug"]
electric_2x_dmglist = ["Water","Fly"]
electric_half_dmglist = ["Electric","Grass","Dragon"]
def dmg_modifier(attack_type, receiver_pokemon_type):
if attack_type == "Normal" and receiver_pokemon_type in normal_2x_dmglist:
print "Normal deals 2x damage!"
elif attack_type == "Normal" and receiver_pokemon_type in normal_half_dmglist:
print "Normal deals 1/2 damage!"
elif attack_type == "Rock" and receiver_pokemon_type in rock_2x_dmglist:
print "Rock deals 2x damage!"
elif attack_type == "Rock" and receiver_pokemon_type in rock_half_dmglist:
print "Rock deals 1/2x damage!"
elif attack_type == "Water" and receiver_pokemon_type in water_2x_dmglist:
print "Water deals 2x damage!"
elif attack_type == "Water" and receiver_pokemon_type in water_half_dmglist:
print "Water deals 1/2x damage!"
elif attack_type == "Grass" and receiver_pokemon_type in grass_2x_dmglist:
print "Grass deals 2x damage!"
elif attack_type == "Grass" and receiver_pokemon_type in grass_half_dmglist:
print "Grass deals 1/2x damage!"
elif attack_type == "Fighting" and receiver_pokemon_type in fighting_2x_dmglist:
print "Fighting deals 2x damage!"
elif attack_type == "Fighting" and receiver_pokemon_type in fighting_half_dmglist:
print "Fighting deals 1/2x damage!"
elif attack_type == "Ground" and receiver_pokemon_type in ground_2x_dmglist:
print "Ground deals 2x damage!"
elif attack_type == "Ground" and receiver_pokemon_type in ground_half_dmglist:
print "Ground deals 1/2x damage!"
elif attack_type == "Electric" and receiver_pokemon_type in electric_2x_dmglist:
print "Electric deals 2x damage!"
elif attack_type == "Electric" and receiver_pokemon_type in electric_half_dmglist:
print "Electric deals 1/2x damage!"
else:
print "Type deals 1x damage - stays the same"
This works perfectly, it's just very repetitive and dirty.
To automate this, I planned to attach the declared "attack type" to the "_2x_dmglist" so that it will automatically call the list.
First I lowercase the declared attack type, for example "Rock", to make it into rock. Then I attach it to _2x_dmglist so that it comes out as rock_2x_dmglist. Then it can find the list.
def dmg_modifier_2(attack_type, receiver_pokemon_type):
lower_case_attack_type = attack_type
lower_cased_attack_type = lower_case_attack_type.lower()
if attack_type == attack_type and receiver_pokemon_type in lower_cased_attack_type+_2x_dmglist:
print attack_type+"deals 2x damage"
elif attack_type == attack_type and receiver_pokemon_type in lower_cased_attack_type+_2x_dmglist:
print attack_type+"deals 1/2 damage"
else:
print "Not working"
Enter this:
dmg_modifier_2("Rock","Rock")
Output:
Traceback (most recent call last):
File "poke_game_test.py", line 98, in <module>
dmg_modifier_2("Rock","Rock")
File "poke_game_test.py", line 90, in dmg_modifier_2
finder = lower_case_attack_type.lower() + _2x_dmglist
NameError: global name '_2x_dmglist' is not defined
I understand the traceback, but how do I concatenate the two variable names so that the whole thing can be automated? In PHP, I was able to do this by just using the + sign, which I tried here. I also understand that the lower cased "Rock" is also a string, while _2x_dmglist is not. I'm just quite confused really if variable concatenation works in python cause I can't find a question similar to it here.
Or is there something fundamentally wrong with my code?
Thanks!