Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Sometimes it may be necessary to
- split a list of players into more than two teams.
- Create a new function which takes as parameters
- a list of team names and a list of players
- (both of these will be lists of strings),
- and allocates players to teams as evenly as
- possible.
- """
- from random import choice, shuffle
- players = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R']
- teams = ["A Team", "B Team", "C Team", "D Team"]
- teamsquads=[]
- players_bk=[]
- for each in range(0, len(players)):
- players_bk.append(players[each])
- def make_teams(teamz, playersz):
- shuffle(playersz)
- while len(playersz) > 0:
- try:
- for team in teamz:
- shuffle(playersz)
- picked_player = playersz.pop()
- print(playersz)
- teamsquads.append(team + " " + picked_player)
- print (teamsquads)
- except:
- break # break out of the loop if no players left
- make_teams(teams, players)
- print(f" players = {players} length {len(players)}")
- print(f" squads = {teamsquads} length {len(teamsquads)}")
- print(f" players_bk = {players} length {len(players)}")
- print(f" teams = {teams} length {len(teams)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement