简体   繁体   中英

How to find the highest value in a specific index (using a specific function?)

I need to create a function called display_highest_chip_holder(player_list) :

This function takes the player list (list of lists) as a parameter and displays the player with the highest 
chip balance in the list of players to the screen. Where two players have the same chip balance, the 
player with the lower games played value should be displayed to the screen. If no players are stored 
in the list or a player with a highest chip balance cannot be found (i.e. all players have a chip balance 
of zero), display an error message accordingly.

player_list :

[['Bruce Wayne', 5, 5, 0, 0, 100, 15], ['Jessica Jones', 12, 0, 6, 6, 10, 6], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]]

player_list[i][1] = Games Played

player_list[i][5] = Amount of Chips each player has 

This is what I have created:

def display_highest_chip_holder(player_list):

    largest_chip = 300

    for i in range(len(player_list)):
         if player_list[i][5] == player_list[i][1]:
             print("Highest Chip Holder =>", player_list[i][0], "with", player_list[i][5], "chips!")
        
        
         elif player_list[i][5] >= largest_chip:
               print("Highest Chip Holder =>", player_list[i][0], "with", player_list[i][5], "chips!")

         elif player_list[i][5] == 0:
                   print("No player has the highest chips.")

However I don't think I have done this correctly, it kind of works but not really as intended and I'm not sure how to do the lower games played value part. I have to use the amount of games played but I don't know how to implement it.

Please note I am unable to use any of the list functions at all aside from choosing a specific index and range.

Thank you!

player_list = [['Bruce Wayne', 2, 5, 0, 0, 500, 15], ['Jessica Jones', 12, 0, 6, 6, 10, 6], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]]
def display_highest_chip_holder(player_list):

    largest_chip = 0
    highest_chip_list = []
    for player in player_list:
        if player[5] > largest_chip:
            largest_chip = player[5]
    
    for i in range(len(player_list)):
        games_played = player_list[i][1]
        amount_of_chip = player_list[i][5]
        if amount_of_chip >= largest_chip:
            highest_chip_list.append(player_list[i])
            
    
    if len(highest_chip_list) > 1:
        games_played = 0
        for player in highest_chip_list:
            if player[1] > games_played:
                games_played = player[1]
        return [player for player in highest_chip_list if player[1] != games_played][0]
    
    return highest_chip_list[0]
            
display_highest_chip_holder(player_list)

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