Advertisement
farrismp

2.4 Multiple Teams

Apr 30th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. # Multiple teams
  2. # creates a list of lists, each nested list being a team
  3. # first it shuffles the list and then selects players using 'choice'
  4. from random import shuffle, choice
  5.  
  6. # Different test lists
  7. players = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  8. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']
  9. # players = ['Harry', 'Hermione', 'Neville', 'Ginny', 'Mick', 'Sheree']
  10. # players = []
  11. # players = ['Harry']
  12. # players = ['Harry', 'Ginny']
  13.  
  14. team_names = [] # a list of team names
  15. teams = [] # will become the nested list of teams
  16.  
  17. # Add a player to each team
  18. def share_players(team_names, players):
  19. # for each team in team_names build string of team name and players
  20. for team in team_names:
  21. tstring = ""
  22. # First add the team name
  23. tstring = tstring + team
  24. # Now add the correct number of players to the team using choice
  25. for player in range(0, int(num_of_players / num_teams)):
  26. player_picked = choice(players)
  27. tstring = tstring + "," + player_picked
  28. players.remove(player_picked)
  29. # now add the string as a list to teams list
  30. teams.append(tstring.split(","))
  31.  
  32. # Start
  33. # How many teams involved
  34. num_teams = int(input('How many Teams ? '))
  35. # Count the number of players available
  36. num_of_players = sum(1 for num_of_players in players)
  37.  
  38. # Test we have correct number of players to share into the teams
  39. if num_of_players % num_teams != 0:
  40. # Provide warning re player numbers
  41. print("Warning {} players cannot split exactly into {} teams !".format(num_of_players, num_teams))
  42. # First check player list isnt empty
  43. if num_of_players == 0:
  44. print("No players to select from !")
  45. else:
  46. # Get the team names
  47. for loop in range(0, num_teams):
  48. team_names.append(input("Name of team {} ? ".format(loop + 1)))
  49.  
  50. # Shuffle the list of players
  51. shuffle(players)
  52. # Now repeatedly add a player to each team
  53. share_players(team_names, players)
  54. # Finally print the team lists
  55. print("The teams are ")
  56. for loop in range(0, num_teams):
  57. print(teams[loop][0])
  58. printstr = ""
  59. for player in teams[loop][1:]:
  60. printstr = printstr + " " + player
  61. print((printstr))
  62. # if there are players left unselected display them
  63. if players:
  64. print("The players not chosen are ")
  65. print(players)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement