Advertisement
KaySawbridge

Adding players to teams

Jul 27th, 2020 (edited)
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. from random import choice#import the choice function from the random library
  2. from random import shuffle #imports the shuffle function from the random library which randomises the order of list
  3. from random import sample #imports the sample function from the random library which randomly select items in list
  4. #create an empty list
  5. teamA =[]
  6. teamB =[]
  7. players =['Alfie', 'Bob', 'Charlie', 'Doug', 'Eric', 'Fred', 'Gareth', 'Harry', 'Ian', 'Jack']
  8.  
  9. shuffle(players) #shuffle names in list
  10.  
  11. #create the add_player_to_team function
  12.  
  13. def add_player_to_team(team):
  14. #pick a player, add them to the team list (A or B) and remove that player from the players list
  15.      player_picked = choice(players)
  16.      team.append(player_picked)
  17.      players.remove(player_picked)
  18.  
  19. add_player_to_team(teamA) #call the add_player_to_teamfunction once
  20. add_player_to_team(teamB)
  21.  
  22. #a loop which adds players to the teams until the players list is empty
  23. while players!=[]:
  24.      add_player_to_team(teamA)
  25.      add_player_to_team(teamB)
  26. print(teamA)
  27. print(teamB)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement