Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Multiple teams
- # creates a list of lists, each nested list being a team
- # first it shuffles the list and then selects players using 'choice'
- from random import shuffle, choice
- # Different test lists
- players = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
- 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']
- # players = ['Harry', 'Hermione', 'Neville', 'Ginny', 'Mick', 'Sheree']
- # players = []
- # players = ['Harry']
- # players = ['Harry', 'Ginny']
- team_names = [] # a list of team names
- teams = [] # will become the nested list of teams
- # Add a player to each team
- def share_players(team_names, players):
- # for each team in team_names build string of team name and players
- for team in team_names:
- tstring = ""
- # First add the team name
- tstring = tstring + team
- # Now add the correct number of players to the team using choice
- for player in range(0, int(num_of_players / num_teams)):
- player_picked = choice(players)
- tstring = tstring + "," + player_picked
- players.remove(player_picked)
- # now add the string as a list to teams list
- teams.append(tstring.split(","))
- # Start
- # How many teams involved
- num_teams = int(input('How many Teams ? '))
- # Count the number of players available
- num_of_players = sum(1 for num_of_players in players)
- # Test we have correct number of players to share into the teams
- if num_of_players % num_teams != 0:
- # Provide warning re player numbers
- print("Warning {} players cannot split exactly into {} teams !".format(num_of_players, num_teams))
- # First check player list isnt empty
- if num_of_players == 0:
- print("No players to select from !")
- else:
- # Get the team names
- for loop in range(0, num_teams):
- team_names.append(input("Name of team {} ? ".format(loop + 1)))
- # Shuffle the list of players
- shuffle(players)
- # Now repeatedly add a player to each team
- share_players(team_names, players)
- # Finally print the team lists
- print("The teams are ")
- for loop in range(0, num_teams):
- print(teams[loop][0])
- printstr = ""
- for player in teams[loop][1:]:
- printstr = printstr + " " + player
- print((printstr))
- # if there are players left unselected display them
- if players:
- print("The players not chosen are ")
- print(players)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement