请原谅大量的代码块,但我根本无法弄清楚出了什么问题。据我了解,这应该工作。焊接清单是随机分配的设备,然后将所有匹配值相加。但是由于某种原因,它会返回大的几乎随机数。我真的需要帮助。
import random
from collections import Counter
def getRandomWeightedElement(**data):
rand = random.randint(1, sum(data.values()))
for key, value in data.items():
rand -= value
if rand <= 0:
return key
equipment = {
"nothing" : Counter({"physA":0}),
"woodenShield" : Counter({"physD":1,"fireD":3}),
#Physical:
#Bronze Weapons
"bronzeDag" : Counter({"physA":12}),
"bronzeSword" : Counter({"physA":23,"physD":2}),
"bronzeBAxe" : Counter({"physA":23}),
"bronze2HSword" : Counter({"physA":26}),
"bronzeMace" : Counter({"physA":26}),
"bronzeWarHammer" : Counter({"physA":26}),
#Iron Weapons
"ironDag" : Counter({"physA":15}),
"ironSword" : Counter({"physA":27,"physD":3}),
"ironBAxe" : Counter({"physA":27}),
"iron2HSword" : Counter({"physA":31}),
"ironMace" : Counter({"physA":31}),
"ironWarHammer" : Counter({"physA":31}),
#Bronze Armor
"bronzeShield" : Counter({"physD":10,"rangD":15,"magD":5}),
"bronzeMedHelm" : Counter({"physD":2,"rangD":4,"magD":1}),
"bronzeFullHelm" : Counter({"physD":4,"rangD":6,"magD":2}),
"bronzeChainbody" : Counter({"physD":6,"rangD":9,"magD":3}),
"bronzePlatebody" : Counter({"physD":7,"rangD":12,"magD":3}),
"bronzeChainlegs" : Counter({"physD":4,"rangD":6,"magD":2}),
"bronzePlatelegs" : Counter({"physD":5,"rangD":8,"magD":2}),
"bronzeBoots" : Counter({"physD":2,"rangD":3,"magD":1}),
#Iron Armor
"ironShield" : Counter({"physD":12,"rangD":18,"magD":6}),
"ironMedHelm" : Counter({"physD":3,"rangD":5,"magD":1}),
"ironFullHelm" : Counter({"physD":4,"rangD":8,"magD":2}),
"ironChainbody" : Counter({"physD":7,"rangD":11,"magD":3}),
"ironPlatebody" : Counter({"physD":9,"rangD":14,"magD":4}),
"ironChainlegs" : Counter({"physD":4,"rangD":8,"magD":2}),
"ironPlatelegs" : Counter({"physD":6,"rangD":9,"magD":3}),
"ironBoots" : Counter({"physD":2,"rangD":4,"magD":1}),
"pineSBow" : Counter({"rangA":22}),
"bronzeNecklace" : Counter({"physD":1}),
"bronzeRing" : Counter({"rangA":0}),
"silverRing" : Counter({"rangA":0})
}
def goblinGen():
"Generates a Goblin, ready to fight"
mainH = getRandomWeightedElement(**{"bronzeDag":40,"pineSBow":25,"bronzeSword":15,"bronzeBAxe":10,"ironSword":10})
offH = getRandomWeightedElement(**{"nothing":50,"woodenShield":40,"bronzeShield":10})
head = getRandomWeightedElement(**{"nothing":80,"bronzeMedHelm":15,"ironMedHelm":5})
neck = getRandomWeightedElement(**{"nothing":90,"bronzeNecklace":10})
chest = getRandomWeightedElement(**{"nothing":60,"bronzeChainbody":40})
legs = getRandomWeightedElement(**{"nothing":75,"bronzeChainlegs":20,"ironChainlegs":5})
gloves = getRandomWeightedElement(**{"nothing":95,"leatherGloves":5})
boots = getRandomWeightedElement(**{"nothing":95,"bronzeBoots":5})
ring1 = getRandomWeightedElement(**{"nothing":95,"bronzeRing":5})
ring2 = getRandomWeightedElement(**{"nothing":95,"silverRing":5})
wielding = [mainH,offH,head,neck,chest,legs,gloves,boots,ring1,ring2]
print("\n",wielding,"\n")
total = equipment["nothing"]
for item in wielding:
total += equipment[item]
print("Physical Attack:",total["physA"],"Physical Defence:",total["physD"])
print("Ranged Attack:", total["rangA"],"Ranged Defence:", total["rangD"])
print("Magic Attack:",total["magA"],"Magic Defence:",total["magD"])
print("Fire Attack Damage",total["fireAD"],"Fire Defence Protection",total["fireDP"])
print("Poison Attack Chance",total["poisAC"],"Poison Defence Chance",total["poisDC"])
print("Prayer Offence Bonus",total["prayOB"],"Prayer Defence Bonus",total["prayDB"])
goblinGen()
答案 0 :(得分:1)
因此,基本问题是您正在对nothing
计数器进行突变,因此当您生成多个小酒杯时,结果会累积。通过实例化新的总计计数器来解决此问题。
我也修复了其他几件事:
Counter
,因为这是不必要的。**
中使用getRandomWeightedElement
字典解包语法。pprint.pprint
用于打印效果很好。)import random
from collections import Counter
from pprint import pprint
def getRandomWeightedElement(data):
rand = random.randint(1, sum(data.values()))
for key, value in data.items():
rand -= value
if rand <= 0:
return key
equipment = {
"nothing": {"physA": 0},
"woodenShield": {"physD": 1, "fireD": 3},
# Physical:
# Bronze Weapons
"bronzeDag": {"physA": 12},
"bronzeSword": {"physA": 23, "physD": 2},
"bronzeBAxe": {"physA": 23},
"bronze2HSword": {"physA": 26},
"bronzeMace": {"physA": 26},
"bronzeWarHammer": {"physA": 26},
# Iron Weapons
"ironDag": {"physA": 15},
"ironSword": {"physA": 27, "physD": 3},
"ironBAxe": {"physA": 27},
"iron2HSword": {"physA": 31},
"ironMace": {"physA": 31},
"ironWarHammer": {"physA": 31},
# Bronze Armor
"bronzeShield": {"physD": 10, "rangD": 15, "magD": 5},
"bronzeMedHelm": {"physD": 2, "rangD": 4, "magD": 1},
"bronzeFullHelm": {"physD": 4, "rangD": 6, "magD": 2},
"bronzeChainbody": {"physD": 6, "rangD": 9, "magD": 3},
"bronzePlatebody": {"physD": 7, "rangD": 12, "magD": 3},
"bronzeChainlegs": {"physD": 4, "rangD": 6, "magD": 2},
"bronzePlatelegs": {"physD": 5, "rangD": 8, "magD": 2},
"bronzeBoots": {"physD": 2, "rangD": 3, "magD": 1},
# Iron Armor
"ironShield": {"physD": 12, "rangD": 18, "magD": 6},
"ironMedHelm": {"physD": 3, "rangD": 5, "magD": 1},
"ironFullHelm": {"physD": 4, "rangD": 8, "magD": 2},
"ironChainbody": {"physD": 7, "rangD": 11, "magD": 3},
"ironPlatebody": {"physD": 9, "rangD": 14, "magD": 4},
"ironChainlegs": {"physD": 4, "rangD": 8, "magD": 2},
"ironPlatelegs": {"physD": 6, "rangD": 9, "magD": 3},
"ironBoots": {"physD": 2, "rangD": 4, "magD": 1},
"pineSBow": {"rangA": 22},
"bronzeNecklace": {"physD": 1},
"bronzeRing": {"rangA": 0},
"silverRing": {"rangA": 0},
}
def goblinGen():
"Generates a Goblin, ready to fight"
wielding = {
'mainH': getRandomWeightedElement({"bronzeDag": 40, "pineSBow": 25, "bronzeSword": 15, "bronzeBAxe": 10, "ironSword": 10}),
'offH': getRandomWeightedElement({"nothing": 50, "woodenShield": 40, "bronzeShield": 10}),
'head': getRandomWeightedElement({"nothing": 80, "bronzeMedHelm": 15, "ironMedHelm": 5}),
'neck': getRandomWeightedElement({"nothing": 90, "bronzeNecklace": 10}),
'chest': getRandomWeightedElement({"nothing": 60, "bronzeChainbody": 40}),
'legs': getRandomWeightedElement({"nothing": 75, "bronzeChainlegs": 20, "ironChainlegs": 5}),
'gloves': getRandomWeightedElement({"nothing": 95, "leatherGloves": 5}),
'boots': getRandomWeightedElement({"nothing": 95, "bronzeBoots": 5}),
'ring1': getRandomWeightedElement({"nothing": 95, "bronzeRing": 5}),
'ring2': getRandomWeightedElement({"nothing": 95, "silverRing": 5}),
}
pprint(wielding)
total = Counter()
for item in wielding.values():
total += equipment[item]
print("Physical Attack:", total["physA"], "Physical Defence:", total["physD"])
print("Ranged Attack:", total["rangA"], "Ranged Defence:", total["rangD"])
print("Magic Attack:", total["magA"], "Magic Defence:", total["magD"])
print("Fire Attack Damage", total["fireAD"], "Fire Defence Protection", total["fireDP"])
print("Poison Attack Chance", total["poisAC"], "Poison Defence Chance", total["poisDC"])
print("Prayer Offence Bonus", total["prayOB"], "Prayer Defence Bonus", total["prayDB"])
goblinGen()
示例输出:
{'boots': 'nothing',
'chest': 'nothing',
'gloves': 'nothing',
'head': 'nothing',
'legs': 'bronzeChainlegs',
'mainH': 'bronzeBAxe',
'neck': 'nothing',
'offH': 'woodenShield',
'ring1': 'nothing',
'ring2': 'nothing'}
Physical Attack: 23 Physical Defence: 5
Ranged Attack: 0 Ranged Defence: 6
Magic Attack: 0 Magic Defence: 2
Fire Attack Damage 0 Fire Defence Protection 0
Poison Attack Chance 0 Poison Defence Chance 0
Prayer Offence Bonus 0 Prayer Defence Bonus 0