私は、ランダムに何かに影響を与える神を作り、オリジナルに関連するものに影響を与えるプログラムを作っています。関連する部分は、以下のとおりです。私はこれを実行するとリストである辞書の値から選択すると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(球['死'))'を意味しますか? – smarx
そうでない場合は、何をしようとしているか説明してください。 (あなたは 'dict'を' random.choice'に渡していますが、これは意味をなさないものです) – smarx