Advertisement
Antypas

Choose a Team

Apr 11th, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. # Adding players to different teams
  2. # Suggested Alternative 2:
  3. '''
  4. Iterate through the list of players, and for each player,
  5. randomly assign them to a team. You’ll need to make sure
  6. that you don’t end up with uneven teams!
  7. '''
  8.  
  9. from random import choice
  10.  
  11. teamA = []
  12. teamB = []    # Create two empty lists for teams A and B
  13. team_list = [teamA, teamB]
  14.  
  15. players = ['Harry', 'Hermione', 'Neville', 'Ginny', 'Myself']
  16. #players = ['Harry', 'Hermione', 'Neville', 'Ginny']
  17. #players = ['Harry', 'Ginny']
  18. #players = ['Harry']
  19. #players = []
  20. #players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']
  21.  
  22. def assign_player_to_team(player):
  23.     team_picked = choice(team_list)
  24.     team_picked.append(player)
  25. #   players.remove(player)
  26.  
  27. for player in players:
  28.     if len(teamA) < len(players)//2 and len(teamB) < len(players)//2:
  29.         assign_player_to_team(player)
  30.     elif len(teamA) > len(teamB):
  31.         teamB.append(player)
  32.     elif len(teamA) < len(teamB):
  33.         teamA.append(player)
  34.     elif len(teamA) == len(teamB):
  35.         assign_player_to_team(player)
  36.    
  37.  
  38. print("teamA ",teamA)
  39. print("teamB ",teamB)
  40. print("Players ", players)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement