我需要創建一個名為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
這是我創建的:
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.")
但是我認為我沒有正確地做到這一點,它有點作業,但并不像預期的那樣,我不確定如何做較低的游戲價值部分。我必須使用玩過的游戲數量,但我不知道如何實作它。
請注意,除了選擇特定的索引和范圍之外,我根本無法使用任何串列功能。
謝謝!
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490565.html
上一篇:C#介面實作中的不同型別