TypeError:list indices必须是整数,而不是list

时间:2013-11-27 16:13:09

标签: python python-3.x

def take_turn(Nplayers, hands, player, pile, turn):
    while finished(hands) is not True:
        pile.append(hands[player][0]) # this line
        hands[player].pop(0)
        while pile[-1] <= 10:
            print(turn, ':', '\nPile:', pile, '\nHands\n', '\n'.join(map(str, hands)), '\n')
            check_players(Nplayers, hands, player, pile, turn)
            turn += 1
            player = (player + 1) % Nplayers
            if len(hands[player]) == 0:
                hands.pop(player)
                Nplayers -= 1
                player = player % Nplayers
            pile.append(hands[player][0])
            hands[player].pop(0)
            if table[-1] > 10:
            break
        penalty_card(Nplayers, hands, player, pile, turn)
    return turn

标记为(#this line)的行返回标题中所述的错误,在我的程序中我将玩家设置为最初等于0,所以应该没有问题吗?

编辑:hands是一个列表列表,player是一个整数

1 个答案:

答案 0 :(得分:0)

如果提供了工作代码示例,那么帮助会更容易。看起来有许多副作用的功能。也许你可以包括一些最小的模拟和一个如下所示的样本调用,以便对问题进行包含,可运行的演示?

def finished(hands):
        if len(hands) == 0:
            return True
        return False


def check_players(*args, **kwargs):
    pass


def penalty_card(*args, **kwargs):
    pass


table = [9]


def take_turn(Nplayers, hands, player, pile, turn):
    while finished(hands) is not True:
        pile.append(hands[player][0]) # this line
        hands[player].pop(0)
        while pile[-1] <= 10:
            print(turn, ':', '\nPile:', pile, '\nHands\n', '\n'.join(map(str, hands)), '\n')
            check_players(Nplayers, hands, player, pile, turn)
            turn += 1
            player = (player + 1) % Nplayers
            if len(hands[player]) == 0:
                hands.pop(player)
                Nplayers -= 1
                player = player % Nplayers
            pile.append(hands[player][0])
            hands[player].pop(0)
            if table[-1] > 10:
                break
        penalty_card(Nplayers, hands, player, pile, turn)
    return turn


take_turn(Nplayers=1, hands=[[1,1,1], [1,1], [1]], player=0, pile=[5, 5], turn=3)