简体   繁体   中英

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

The line marked by the (# this line) returns the error as stated in the title, in my programme I have set player to initially equal 0, so there should be no problems right?

Edit: hands is a list of lists, player is an integer

It would be a lot easier to help if a working code sample were provided. It looks like there's a number of functions with side effects. Perhaps you could include some minimal mocks and a sample call like below to make a contained, runnable demonstration of the problem?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM