Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2 5 UsingLists2.py
- # with "choice" and a method to strip square brackets, quotes.
- from random import choice
- # Note: Output will be different everytime as it returns a random item.
- teamA = [] # Create two empty lists for teams A and B
- teamB = []
- players = ['Afiki', 'Bozo', 'Ching', 'Don', 'Emmet', 'Fox', 'Shoes' ]
- # print("Players to choose from :", players) # Prints the [, ], and ',
- # Note, strip() does not work on a list !!
- # from: https://howtodoinjava.com/python/examples/python-print-list/
- # Use join(map(str, method on the list items to strip [ ] Yay!
- print('Players List :', ', '.join(map(str, players))) # Very useful. I feel a def coming on.
- # If required Adds a VACANT player to make an even number
- if len(players)%2 != 0: # if number of players is odd,
- players.append('VACANT') # add a dummy player
- def add_player_to_team(team):
- player_picked = choice(players) #Return a random element from a list
- team.append(player_picked)
- players.remove(player_picked)
- # Loop through players list to allocate 1 player per team per loop
- for i in range(int(len(players)/2)):
- add_player_to_team(teamA)
- add_player_to_team(teamB)
- # This above code will now add one player to team A, and one to team B
- # and repeat until done.
- #_______________________________________
- ''' Repeating for all players
- Now that you’ve worked out how to add players to teams, you need to keep going until all of the players have been allocated to one of the teams.
- Using a loop, and making sure to not try to pick a player from an empty list,
- finish off this program to split the players into two teams, teamA and teamB.
- Make sure to print the two teams at the end of the program to make sure it’s worked, and share your code in the comments. '''
- print("teamA :",teamA)
- print("teamB :",teamB)
- print("Players remaining in list after choosing :",players)
- ''' first run with even num of players
- >>>
- ['Afiki', 'Bozo', 'Ching', 'Don', 'Emmet', 'Fox']
- ['Bozo', 'Afiki', 'Fox']
- ['Emmet', 'Don', 'Ching']
- []
- >>> '''
- ''' second run with odd num of players
- >>>
- ['Afiki', 'Bozo', 'Ching', 'Don', 'Emmet', 'Fox', 'Shoes']
- ['Emmet', 'Don', 'Shoes', 'Afiki']
- ['Fox', 'Ching', 'Bozo', 'VACANT']
- []
- >>> '''
- # Note: How to strip the sq bkts and quotes from the print() ??
- # Use join(map(str as above
- # see https://ipcisco.com/lesson/python-list-to-string/ has examples
- '''
- >>>
- Players List : Afiki, Bozo, Ching, Don, Emmet, Fox, Shoes
- teamA : ['VACANT', 'Don', 'Ching', 'Bozo']
- teamB : ['Emmet', 'Fox', 'Shoes', 'Afiki']
- Players remaining in list after choosing : []
- >>> '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement