Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- multiple teams
- function takes a list of team names and a list of players and divides them equally
- '''
- from random import choice # selects a random sample from a list
- names=["teama", "teamb", "teamc", "teamd"]
- players=["fred", "harry", "bert", "tom", "pete", "kevin", "debbie", "gene", "ian", "terry", "ralph"]
- #Q.What structure will the final data? A.list of lists.
- #Team list will have team name as first element, followed by player names
- def create_teams(names, players):
- teams=[]
- num_teams=len(names)
- for i, name in enumerate(names): #build list of lists
- teams.append([]) #the team list is part of the teams list
- teams[i].append(name.upper()) #first element of team list is team name in CAPS
- i=0 #used to select teams
- while players: #until players is empty
- index=i%num_teams #pick each team in turn
- player=choice(players) #pick a player
- players.remove(player) #remove from players list
- teams[index].append(player.capitalize()) #add to correct list
- i+=1 #move to next team in list
- return teams
- #test
- print(create_teams(names,players))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement