Advertisement
timber101

players and teams

Dec 24th, 2020
1,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. """
  2. Sometimes it may be necessary to
  3. split a list of players into more than two teams.
  4. Create a new function which takes as parameters
  5. a list of team names and a list of players
  6. (both of these will be lists of strings),
  7. and allocates players to teams as evenly as
  8. possible.
  9.  
  10. """
  11. from random import choice, shuffle
  12.  
  13. players = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R']
  14.  
  15. teams = ["A Team", "B Team", "C Team", "D Team"]
  16.  
  17. teamsquads=[]
  18.  
  19. players_bk=[]
  20. for each in range(0, len(players)):
  21.     players_bk.append(players[each])
  22.  
  23. def make_teams(teamz, playersz):
  24.     shuffle(playersz)
  25.    
  26.     while len(playersz) > 0:
  27.         try:
  28.             for team in teamz:
  29.                 shuffle(playersz)
  30.                 picked_player = playersz.pop()
  31.                 print(playersz)
  32.                 teamsquads.append(team + " " + picked_player)
  33.                 print (teamsquads)
  34.         except:
  35.             break # break out of the loop if no players left
  36.    
  37.    
  38. make_teams(teams, players)
  39.  
  40. print(f" players =  {players} length {len(players)}")
  41. print(f" squads =  {teamsquads} length {len(teamsquads)}")
  42. print(f" players_bk =  {players} length {len(players)}")
  43. print(f" teams =  {teams} length {len(teams)}")
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement