Advertisement
timber101

PickNoOfPplayers

Dec 24th, 2020
1,037
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None
  1. """
  2. Work out the number of players needed in one team,
  3. and use the sample function to randomly pick that number of players
  4. from the list.
  5. Then add the rest of the players to the second team.
  6. You’ll need to import the sample function from the random library.
  7. The following code would set my_sample to be a list containing
  8. three random elements from the list my_list:
  9.  
  10. from random import sample
  11.        
  12. my_sample = sample(my_list,3)
  13. # Randomly select 3 items from my_list and creates my_sample
  14.  
  15. """
  16. from random import sample
  17.  
  18. teamA = [] # empty list for team A
  19. teamB = [] # do for team B
  20.  
  21. players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']
  22.  
  23. #  to preserve my original players list appending each element to teamB
  24. for i in range(0, len(players)):
  25.     teamB.append(players[i])
  26.  
  27. def make_teams(noz):
  28.     tempA = (sample(players,noz))
  29.     for i in range(0,len(tempA)):
  30.         teamA.append(tempA[i])
  31.         #print(f"tempA {tempA}") for debugging
  32.         #print(f"teamA {teamA}") for debugging
  33.         #print(f"teamB {teamB}") for debugging
  34.         #print(f"players {players}") for debugging
  35.         teamB.remove(tempA[i])
  36.         #print(f"teamB {teamB}") for debugging
  37.    
  38. make_teams(2)
  39.  
  40. print(f"teamA final {teamA} of length {len(teamA)}")
  41. print(f"teamB final {teamB} of length {len(teamB)}")
  42. print(f"players final {players} of length {len(players)}")
Advertisement
RAW Paste Data Copied
Advertisement