I am trying to make a scoreboard based on an input which has a list of all rounds played and their result. I have already split this input so that each round of games is one list; in it, one item is one game played. Each game is noted as ‘White Black ResultWhite ResultBlack’.
For example, with three rounds and six players, it is as follows. The first list is the players, followed by the rounds (2-4). 1 = win, 0.5 = tie, 0 = loss.
[['Erik', 'Daniel', 'Charlotte', 'Anna', 'Bob', 'Femke'], ['Anna Femke 1 0', 'Bob Erik 0 1', 'Charlotte Daniel 0.5 0.5'], ['Erik Anna 1 0', 'Femke Charlotte 0.5 0.5', 'Daniel Bob 1 0'], ['Daniel Erik 0.5 0.5', 'Charlotte Anna 1 0', 'Bob Femke 1 0']]
Now, I want to:
- Identify the first list as names in a class called player_result
- Ignore the first list of names from this point onwards
- Split the list of all rounds into individual lists
- Add ResultWhite (index 2) to the points in said class of White (index 0) and vice verse for ResultBlack and Black
I have managed to reach the first three points with the code below, but I cannot resolve the last remark. I have looked into append but I cannot figure it out. If anyone can help it would be great!
class player_result: def __init__(self, name: str): self.name = name def determine_output(input): splits = input.split("nn") rounds = [i.split('n') for i in splits] print(rounds) playernames = rounds[0] players = [] for name in playernames: players.append(player_result(name)) print('Player:', players[0].name) amount_rounds = len(rounds) - 1 print('Rounds:', amount_rounds) for i in range(len(rounds) - 1): print('Round',i + 1,rounds[i + 1])
Edit: some more clarification on the end result. My idea is that the list is being registered as [White Black ResultWhite ResultBlack], and the output below is being presented by all White and Black being player, with the total of their points received. The output / result that I am after would look like the following. This would be achieved by the name being the names item of class player_result, and the score being a new points item of class player_result.
Erik: 2.5 Daniel: 2 Charlotte: 2 Anna: 1 Bob: 1 Femke: 0.5
Advertisement
Answer
The step you are missing is to make a dict
keyed on the players name. This will allow you to look up each player to accrue their scores. Also I have added a score
attribute to player_result
.
Also note that I have kept your original list that you posted rather than the text that your function determine_output()
currently expects.
class player_result: def __init__(self, name): self.name = name self.score = 0 def determine_output(rounds): #splits = input.split("nn") #rounds = [i.split('n') for i in splits] print(rounds) players = {} for name in rounds[0]: players[name] = player_result(name) amount_rounds = len(rounds) - 1 print('Rounds:', amount_rounds) for one_round in rounds[1:]: for game in one_round: white,black,result_white, result_black = game.split() players[white].score += float(result_white) players[black].score += float(result_black) for player in players.values(): print(f'{player.name}: {player.score}') rounds = [['Erik', 'Daniel', 'Charlotte', 'Anna', 'Bob', 'Femke'], ['Anna Femke 1 0', 'Bob Erik 0 1', 'Charlotte Daniel 0.5 0.5'], ['Erik Anna 1 0', 'Femke Charlotte 0.5 0.5', 'Daniel Bob 1 0'], ['Daniel Erik 0.5 0.5', 'Charlotte Anna 1 0', 'Bob Femke 1 0']] determine_output(rounds)
Output as requested.