2017-10-07 5 views
1

私は、ランダムに何かに影響を与える神を作り、オリジナルに関連するものに影響を与えるプログラムを作っています。関連する部分は、以下のとおりです。私はこれを実行するとリストである辞書の値から選択すると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 

であっても、それは同じエラーを生成します行のリスト()なし。私はそれが

+0

あなたは 'random.choice(球['死'))'を意味しますか? – smarx

+2

そうでない場合は、何をしようとしているか説明してください。 (あなたは 'dict'を' random.choice'に渡していますが、これは意味をなさないものです) – smarx

答えて

2
import random 

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, name, associations = None): 
    self.sphere = sphere 
    #so associations are not allocated twice 
    self.possible_associations = spheres[self.sphere] 
    self.associations = associations 
    self.get_association() 
    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 and self.possible_associations: 
     choice = random.choice(self.possible_associations) 
     self.possible_associations.remove(choice) 
     if random.randint(1, 2) == 1: 
     chance_of_association += 1 
     list_of_choices.append(choice) 
    self.associations = list_of_choices 

あなたは「diety1」を参照して、オブジェクトがまだ作成されていないので、それがインスタンス化中、方法、「GET_ASSOCIATION」だ呼び出すことはできません。そこで、__init__が実行されたときにメソッド呼び出しを実行しました。辞書の中のキーのリストを検索するためにrandom.choiceを変更しなければなりませんでした。

deity1 = God(random.choice(list(spheres.keys())), 'godname') 
+0

ありがとう –

2

あなたは

deity1 = God(random.choice(spheres['death']), deity1.get_association, 'godname') 

として行を変更することができ得る。しかし、これはまた別のエラーを作るにはどうすればよいので、もう一度あなたのコードで見てください。

Traceback (most recent call last): 
File "<input>", line 23, in <module> 
NameError: name 'deity1' is not defined 
関連する問題