Advertisement
TonyMo

2_5_usinglists2choice.py

Mar 23rd, 2021
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. # 2 5 UsingLists2.py
  2. # with "choice" and a method to strip square brackets, quotes.
  3.  
  4. from random import choice
  5. # Note: Output will be different everytime as it returns a random item.
  6.  
  7. teamA = []  # Create two empty lists for teams A and B
  8. teamB = []
  9.  
  10. players = ['Afiki', 'Bozo', 'Ching', 'Don', 'Emmet', 'Fox', 'Shoes' ]
  11.  
  12. # print("Players to choose from :", players) # Prints the [, ], and ',
  13.  
  14. # Note, strip() does not work on a list !!
  15. # from: https://howtodoinjava.com/python/examples/python-print-list/
  16. #  Use join(map(str, method on the list items to strip [ ] Yay!
  17. print('Players List :', ', '.join(map(str, players))) # Very useful. I feel a def coming on.
  18.  
  19. # If required Adds a VACANT player to make an even number
  20. if len(players)%2 != 0:        #  if number of players is odd,
  21.     players.append('VACANT')   # add a dummy player
  22.  
  23. def add_player_to_team(team):
  24.     player_picked = choice(players)    #Return a random element from a list
  25.     team.append(player_picked)
  26.     players.remove(player_picked)
  27.  
  28. # Loop through players list to allocate 1 player per team per loop
  29. for i in range(int(len(players)/2)):
  30.     add_player_to_team(teamA)
  31.     add_player_to_team(teamB)
  32.  
  33. # This above code will now add one player to team A, and one to team B
  34. # and repeat until done.
  35. #_______________________________________
  36.  
  37. ''' Repeating for all players
  38.  
  39. 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.
  40. Using a loop, and making sure to not try to pick a player from an empty list,
  41. finish off this program to split the players into two teams, teamA and teamB.
  42. 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. '''
  43.  
  44.  
  45. print("teamA :",teamA)
  46. print("teamB :",teamB)
  47. print("Players remaining in list after choosing :",players)
  48.  
  49. ''' first run with even num of players
  50. >>>
  51. ['Afiki', 'Bozo', 'Ching', 'Don', 'Emmet', 'Fox']
  52. ['Bozo', 'Afiki', 'Fox']
  53. ['Emmet', 'Don', 'Ching']
  54. []
  55. >>> '''
  56.  
  57. ''' second run with odd num of players
  58. >>>
  59. ['Afiki', 'Bozo', 'Ching', 'Don', 'Emmet', 'Fox', 'Shoes']
  60. ['Emmet', 'Don', 'Shoes', 'Afiki']
  61. ['Fox', 'Ching', 'Bozo', 'VACANT']
  62. []
  63. >>> '''
  64. # Note: How to strip the sq bkts and quotes from the print() ??
  65. # Use join(map(str as above
  66. # see https://ipcisco.com/lesson/python-list-to-string/  has examples
  67. '''
  68. >>>
  69. Players List : Afiki, Bozo, Ching, Don, Emmet, Fox, Shoes
  70. teamA : ['VACANT', 'Don', 'Ching', 'Bozo']
  71. teamB : ['Emmet', 'Fox', 'Shoes', 'Afiki']
  72. Players remaining in list after choosing : []
  73. >>> '''
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement