我有一系列的最终给予的列表,用含有数,从字典导出的第一项功能,并且所述第二和第三项是字典。存储随机产生的字典在Python
这些词典以前是随机生成的。
我使用的函数生成这些字典的给定数量,试图获得最多可能的,因为第一个项目。 (它旨在优化骰子卷)。
这一切工作正常,我可以从所有重复打印最高的第一个项目的价值。但是,当我尝试打印与第一个数字相关的两个字典时(记住它们都在一个列表中),它似乎随机生成另外两个字典。
def repeat(type, times):
best = 0
for i in range(0, times):
x = rollForCharacter(type)
if x[0] > best:
print("BEST:", x)
best = x[0]
print("The highest average success is", best)
return best
这很好用。显示的最后一件事是:
BEST: (3.58, [{'strength': 4, 'intelligence': 1, 'charisma': 1, 'stamina': 4, 'willpower': 2, 'dexterity': 2, 'wits': 5, 'luck': 2}, {'agility': 1, 'brawl': 2, 'investigation': 3, 'larceny': 0, 'melee': 1, 'survival': 0, 'alchemy': 3, 'archery': 0, 'crafting': 0, 'drive': 1, 'magic': 0, 'medicine': 0, 'commercial': 0, 'esteem': 5, 'instruction': 2, 'intimidation': 2, 'persuasion': 0, 'seduction': 0}]) The highest average success is 3.58
但如果我尝试的东西来存储这些都给这个号码列表:
def repeat(type, times):
best = 0
bestChar = []
for i in range(0, times):
x = rollForCharacter(type)
if x[0] > best:
print("BEST:", x)
best = x[0]
bestChar = x
print("The highest average success is", best)
print("Therefore the best character is", bestChar)
return best, bestChar
我得到这个作为最后的结果,这是很好的:
BEST: (4.15, [{'strength': 2, 'intelligence': 3, 'charisma': 4, 'stamina': 4, 'willpower': 1, 'dexterity': 2, 'wits': 4, 'luck': 1}, {'agility': 1, 'brawl': 0, 'investigation': 5, 'larceny': 0, 'melee': 0, 'survival': 0, 'alchemy': 7, 'archery': 0, 'crafting': 0, 'drive': 0, 'magic': 0, 'medicine': 0, 'commercial': 1, 'esteem': 0, 'instruction': 3, 'intimidation': 0, 'persuasion': 0, 'seduction': 0}]) The highest average success is 4.15
但最后一行是
Therefore the best character is (4.15, [{'strength': 1, 'intelligence': 3, 'charisma': 4, 'stamina': 4, 'willpower': 1, 'dexterity': 2, 'wits': 2, 'luck': 3}, {'agility': 1, 'brawl': 0, 'investigation': 1, 'larceny': 4, 'melee': 2, 'survival': 0, 'alchemy': 2, 'archery': 4, 'crafting': 0, 'drive': 0, 'magic': 0, 'medicine': 0, 'commercial': 1, 'esteem': 0, 'instruction': 0, 'intimidation': 2, 'persuasion': 1, 'seduction': 0}])
正如您所看到的,这与我想要的不符,以及它在字面上的正确位置。
通过检查的一点点,我体会到了什么给出的“最好的品德”只是最后一个产生的,这是不是最好的,只是最近。然而,并不是那么简单,因为第一个元素是记录的最高结果,而不是来自列表中其余部分的字符。这真是令人困惑,因为它意味着列表以某种方式被编辑,但在任何时候我都无法看到会发生什么。
我是不是在做一些愚蠢的事情,每次随机生成这个角色?我不这么认为,因为x[0]
给出了正确的结果并且存储的很好,所以当它是整个列表时会发生什么变化?
从功能rollForCharacter()返回rollResult, character
这仅仅是数量,然后将两个字典。
我将不胜感激,如果任何人都可以找出并解释我要去哪里错了,为什么它可以打印的正确答案控制台尚未它下面存储正确的线路!
编辑:
字典1个代码:
attributes = {}
def assignRow(row, p): # p is the number of points you have to assign to each row
rowValues = {}
for i in range(0, len(row)-1):
val = randint(0, p)
rowValues[row[i]] = val + 1
p -= val
rowValues[row[-1]] = p + 1
return attributes.update(rowValues)
def getPoints():
points = [7, 5, 3]
shuffle(points)
row1 = ['strength', 'intelligence', 'charisma']
row2 = ['stamina', 'willpower']
row3 = ['dexterity', 'wits', 'luck']
for i in range(0, len(points)):
row = eval("row" + str(i+1))
assignRow(row, points[i])
字典2代码:
skills = {}
def assignRow(row, p): # p is the number of points you have to assign to each row
rowValues = {}
for i in range(0, len(row) - 1):
val = randint(0, p)
rowValues[row[i]] = val
p -= val
rowValues[row[-1]] = p
return skills.update(rowValues)
def getPoints():
points = [11, 7, 4]
shuffle(points)
row1 = ['agility', 'brawl', 'investigation', 'larceny', 'melee', 'survival']
row2 = ['alchemy', 'archery', 'crafting', 'drive', 'magic', 'medicine']
row3 = ['commercial', 'esteem', 'instruction', 'intimidation', 'persuasion', 'seduction']
for i in range(0, len(points)):
row = eval("row" + str(i + 1))
assignRow(row, points[i])
你可以发布你用来生成字典的代码吗? – perigon