簡體   English   中英

TypeError:列表索引必須是整數,而不是list

[英]TypeError: list indices must be integers, not list

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

用(#這行)標記的行返回標題中所述的錯誤,在我的程序中,我已將播放器初始設置為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)

暫無
暫無

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

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