2015-10-07 83 views
0

閱讀其他答案,這一點,但不能完全弄清楚如何將它們應用到我的問題:獲取類型錯誤:列表索引必須是整數,而不是元組,想不通爲什麼

class Dice: 
    def __init__(self): 
     self.dice = [0]*5 
     self.rollAll() 
    def roll(self, which): 
     for pos in enumerate(which): 
      self.dice[pos] = random.randrange(1,7) 
    def rollAll(self): 
     self.roll(range(5)) 
    def values(self): 
     return self.dice[:] 
    def counts(self): 
     # Create counts list 
     counts = [0] * 7 
     for value in self.dice: 
      counts[value] = counts[value] + 1 
     # score the hand 

唐不知道是什麼原因導致了這個錯誤 - 我從其他文章中收集到的信息表明,它與我輸入self.dice位置行的方式有關,但同樣無法計算出究竟是什麼錯誤。你們能幫忙嗎?謝謝!

回答

3
for pos in enumerate(which): 
     self.dice[pos] = random.randrange(1,7) 

枚舉返回(指數值)元組,你需要解壓:

for idx, pos in enumerate(which): 
     self.dice[idx] = random.randrange(1,7) 
+2

我想他們可能要完全忽略'enumerate'; 'rollAll'的示例通過'range'來定義滾動的位置,所以索引不是重要的;大概你可以調用'.roll((3,2,5))',它會按照這個順序滾動3,2和5(使用索引意味着它會滾動0,1和2,這看起來不對)。 – ShadowRanger

+0

謝謝!覺得這麼愚蠢了一陣子哈哈。 – n1c9

+1

是的,我同意我們應該可以做到@ ShadowRanger;事實上,我首先有一個建議,直接重複「哪些」的值,但由於不清楚究竟是「哪個」代表,我刪除了這個建議。 –

相關問題