n1c9
n1c9

Reputation: 2697

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

Read the other answers to this but couldn't quite figure out how to apply them to my problem:

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

Don't get what's causing this error - I've gleaned from other posts like this that it has something to do with the way I've typed my self.dice position line, but again, can't figure out what exactly is wrong. Can any of you help? Thanks!

Upvotes: 0

Views: 64

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36732

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

enumerate returns a tuple of (index, value) that you need to unpack:

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

Upvotes: 3

Related Questions