robbie2301
robbie2301

Reputation: 47

trying to print a leaderboard in python

I am working on a college project and am trying to create a tournament system for academic and sports 'football, netball, maths, english...' The idea is to register 1-20 teams into a tournament and then input winners. The program asks the user who won the game and then would +1 score to them. Now i want to be able to print a leaderboard so that the user can see the 'standings' of the tournament. Can someone please help me with this?

'''

class Team:

    #setting this outside of a method allows us to get it or set it outside of the class in the main code like a "global" for the class.
    TeamName = ''

    def __init__(self, TeamNum, TeamName, TeamMembers):
        self.TeamNum = TeamNum
        self.TeamName = TeamName
        self.TeamMembers = TeamMembers

    def __repr__(self):
        return f'Team Number: {self.TeamNum} |-| Team Name: {self.TeamName} |-| Member Count: {self.TeamMembers} |-| Team Score: {self.TeamScore}'

class Game:
    
    def __init__(self, game_number, home_team, away_team, home_team_score, away_team_score):
        self.home_team = home_team
        self.away_team = away_team
        self.home_team_score = home_team_score
        self.away_team_score = away_team_score

    #a class method that returns the winner based on the scores
    def winner(self):
        if self.home_team_score > self.away_team_score:
            return self.home_team
        else:
            return self.away_team

#instead of lists, use dictionaries so you can refer to a game by its number, or a team by its name without having to jump through hoops. 
games={}
teams = {}

TeamCounter=int(input('How many Teams will be in the tournament? '))

print('')
for i in range(TeamCounter):
    NameOfTeam=input(f'Please Enter Team {i+1} Name: ')
    MemberCount=input('How Many Members in Team? ')
    print('')
    #Add to the teams dictionary.
    teams[NameOfTeam] = Team( i+1, NameOfTeam, MemberCount) 


#Lets collect info about the games in the tourny:
GameCounter=int(input("How many games are in the tournament?"))
for i in range(GameCounter):
    home_team = input("what was the name of the home team?")
    home_team_score = input("what was their score?")
    away_team = input("what was the name of the away team?")
    away_team_score = input("what was their score?")

    #create the game object
    this_game = Game(i, teams[home_team], teams[away_team], home_team_score, away_team_score)

    #Once we have this one game made we can see who won. Note that we call the method `winner()` for `this_game` object which returns a `Team` object for which we can get it's attribute `TeamName`: 
    print("It looks like "+this_game.winner().TeamName+" won the game!")

#this is where i want to add a leaderboard, to print the final standings of the tournament

'''

Upvotes: 0

Views: 500

Answers (1)

You want to keep track for each team, how many times they won.

One way is to have a dictionary where key is of type Team (alternatively string - team name) and value is the number of won games. Lets save this into team_won_games_count = {}. At the end of second loop, when you print the winner, you could also increment amount of won games.

in first loop

   team = Team( i+1, NameOfTeam, MemberCount) 
   teams[NameOfTeam] = team
   # initialize new team in the new dictionary with 0 wins
   team_won_games_count[team] = 0

in second loop

   winner = this_game.winner()
   print("It looks like "+winner.TeamName+" won the game!")
   # add 1 to won games
   team_won_games_count[winner] += 1

Now you can loop the new dictionary at the end to print the name and their scores, e.g.

for team, won_games_count in team_won_games_count.items():
   print("team "+team.TeamName+" | "+won_games_count+" wins")

Another possibility is to add number of wins directly to the team

class Team:
    def __init__(self, TeamNum, TeamName, TeamMembers):
        #...
        self.WonGamesCount = 0

in second loop

   winner = this_game.winner()
   print("It looks like "+winner.TeamName+" won the game!")
   # add 1 to won games
   winner.WonGamesCount += 1

and print leaderboard looping over teams dictionary

You might also want to leaderboard to be sorted (team with most wins first). For that you could use sorted function with key argument:

(with first approach)

# lambda accesses the value of dictionary as a key for sorting the key-value pairs
for team, won_games_count in sorted(team_won_games_count.items(), key=lambda pair: pair[1], reverse=True):
   print("team "+team.TeamName+" | "+won_games_count+" wins")

Upvotes: 1

Related Questions