gpt4 book ai didi

python - 连接两个变量名称以自动在列表 python 中查找内容

转载 作者:行者123 更新时间:2023-11-28 20:40:45 26 4
gpt4 key购买 nike

我是初学者,正在通过制作口袋妖怪战斗模拟器来练习 Python,并且我正在创建伤害修改函数。

思路是:

如果宠物小 Sprite 的“攻击类型”在“attack_type_2x_dmglist”中找到,则宠物小 Sprite 造成两倍的伤害。 “非常有效”

如果宠物小 Sprite 的“攻击类型”在“attack_type_half_dmglist”中找到,则宠物小 Sprite 只会造成一半的伤害。 “不是很有效”

我现在使用 print 来简化调试。

为了验证这一点,我已经完成了类型与类型的条件判断。

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"

这很完美,只是非常重复和肮脏。

为了自动执行此操作,我计划将声明的“攻击类型”附加到“_2x_dmglist”,以便它自动调用该列表。

首先我将声明的攻击类型小写,例如“Rock”,使其变成rock。然后,我将其附加到 _2x_dmglist,这样它就会以 rock_2x_dmglist 的形式出现。然后它可以找到列表。

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"

输入:

dmg_modifier_2("Rock","Rock")

输出:

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

我理解回溯,但我如何连接两个变量名以便整个过程自动化?在 PHP 中,我可以通过使用 + 号来做到这一点,我在这里尝试过。我也明白小写的“Rock”也是一个字符串,而 _2x_dmglist 不是。如果变量连接在 python 中有效,我真的很困惑,因为我在这里找不到类似的问题。

或者我的代码存在根本性错误?

谢谢!

最佳答案

我觉得是时候介绍一下了classes , 但你也可以试试字典:

dict_2x_dmg = {
"Normal":set([]),
"Rock":{"Fire","Ice","Fly","Bug"},
"Water":{"Fire","Ground","Rock"},
"Grass":{"Water","Ground","Rock"},
"Fighting":{"Normal","Ice","Rock","Dark","Steel"},
"Ground":{"Fire","Electric","Poison","Rock","Steel"},
"Electric":{"Water","Fly"}
}

dict_half_dmg = {
"Normal":{"Rock","Dragon"},
"Rock":{"Fighting","Ground","Steel"},
"Water":{"Water","Grass","Dragon"},
"Grass":{"Fire","Grass","Poison","Fly","Bug","Dragon","Steel"},
"Fighting":{"Poison","Fly","Psychic","Bug","Fairy"},
"Ground":{"Grass","Bug"},
"Electric":{"Electric","Grass","Dragon"}
}

def dmg_modifier(attack_type, receiver_pokemon_type):
if receiver_pokemon_type in dict_2x_dmg[attack_type]:
dmg = "2x"
elif receiver_pokemon_type in dict_half_dmg[attack_type]:
dmg = "1/2x"
else:
dmg = "1x"

print "{0} deals {1} damage".format(attack_type, dmg)


>>> dmg_modifier("Water", "Ground")
Water deals 2x damage

您可以做的另一件事 - 是使用字典中的字典:

dict_dmg = {
"Normal":{"Rock":0.5,"Dragon":0.5},
"Rock":{"Fighting":0.5,"Ground":0.5,"Steel":0.5,"Fire":2,"Ice":2,"Fly":2,"Bug":2},
"Water":{"Water":0.5,"Grass":0.5,"Dragon":0.5,"Fire":2,"Ground":2,"Rock":2},
"Grass":{"Fire":0.5,"Grass":0.5,"Poison":0.5,"Fly":0.5,"Bug":0.5,"Dragon":0.5,"Steel":0.5,"Water":2,"Ground":2,"Rock":2},
"Fighting":{"Poison":0.5,"Fly":0.5,"Psychic":0.5,"Bug":0.5,"Fairy":0.5,"Normal":2,"Ice":2,"Rock":2,"Dark":2,"Steel":2},
"Ground":{"Grass":0.5,"Bug":0.5,"Fire":2,"Electric":2,"Poison":2,"Rock":2,"Steel":2},
"Electric":{"Electric":0.5,"Grass":0.5,"Dragon":0.5,"Water":2,"Fly":2}
}

def dmg_modifier2(attack_type, receiver_pokemon_type, dict_dmg):
dmg = dict_dmg[attack_type].get(receiver_pokemon_type, 1)

print "{0} deals {1}x damage".format(attack_type, dmg)

或者您甚至可以使用 pandas DataFrame :

>>> import pandas as pd
>>> df_dmg = pd.DataFrame.from_dict(dict_dmg)
>>> df
Electric Fighting Grass Ground Normal Rock Water
Bug NaN 0.5 0.5 0.5 NaN 2.0 NaN
Dark NaN 2.0 NaN NaN NaN NaN NaN
Dragon 0.5 NaN 0.5 NaN 0.5 NaN 0.5
Electric 0.5 NaN NaN 2.0 NaN NaN NaN
Fairy NaN 0.5 NaN NaN NaN NaN NaN
Fighting NaN NaN NaN NaN NaN 0.5 NaN
Fire NaN NaN 0.5 2.0 NaN 2.0 2.0
Fly 2.0 0.5 0.5 NaN NaN 2.0 NaN
Grass 0.5 NaN 0.5 0.5 NaN NaN 0.5
Ground NaN NaN 2.0 NaN NaN 0.5 2.0
Ice NaN 2.0 NaN NaN NaN 2.0 NaN
Normal NaN 2.0 NaN NaN NaN NaN NaN
Poison NaN 0.5 0.5 2.0 NaN NaN NaN
Psychic NaN 0.5 NaN NaN NaN NaN NaN
Rock NaN 2.0 2.0 2.0 0.5 NaN 2.0
Steel NaN 2.0 0.5 2.0 NaN 0.5 NaN
Water 2.0 NaN 2.0 NaN NaN NaN 0.5

def dmg_modifier3(attack_type, receiver_pokemon_type, df_dmg):
dmg = df_dmg[attack_type][receiver_pokemon_type]

print "{0} deals {1}x damage".format(attack_type, dmg)

关于python - 连接两个变量名称以自动在列表 python 中查找内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com