簡體   English   中英

獲取TypeError:列表索引必須是整數,而不是元組,無法弄清原因

[英]Getting TypeError: list indices must be integers, not tuple, can't figure out why

閱讀有關此問題的其他答案,但無法完全弄清楚如何將其應用於我的問題:

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位置線的方式有關,但同樣,我無法弄清楚到底什么錯誤。 你們有什么可以幫助的嗎? 謝謝!

    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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM