我正在製作一個程序,隨機制作一個影響事物的上帝,並影響與原文相關的事物。相關配件有:random.choice當從字典中選擇一個值列表時不起作用
spheres = {'death': ['death', 'corpses', 'skulls', 'rot', 'ruin', 'the end']}
# Dictionary of the things gods have power over and the relevant things to do with them
class God(object):
def __init__(self, sphere, associations, name):
self.sphere = sphere
self.associations = accociations
self.name = name
# Chooses areas to have power over, hopefully making it less and less likely as the program goes on further
def get_association(self):
chance_of_association = 0
list_of_choices = []
while random.randint(0, chance_of_association) == 0:
choice = random.choice(list(spheres[self.sphere]))
# this is the problem
if random.randint(1, 2) == 1:
chance_of_association += 1
list_of_choices.append(choice)
self.associations = list_of_choices
deity1 = God(random.choice(spheres), deity1.get_association, 'godname')
當我跑,我得到:
File "program.py", line 22, in <module>
deity1 = God(random.choice(spheres), deity1.get_association, 'godname')
File "/opt/python-3.6/lib/python3.6/random.py", line 258, in choice
return seq[i]
KeyError: 0
即使沒有它產生同樣的錯誤就行了列表()。我怎樣才能得到它
你的意思是'random.choice(spheres ['death'])'? – smarx
如果不是,請解釋你正在嘗試做什麼。 (你正在向'random.choice'傳遞一個'dict',這沒有任何意義。) – smarx