farrismp

Shuffle and Pop

Apr 29th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. # Select teams by using shuffle
  2. from random import shuffle
  3.  
  4. teamA = []
  5. teamB = [] # Create two empty lists for teams A and B
  6.  
  7. players = ['Harry', 'Hermione', 'Neville', 'Ginny', 'Mick', 'Sheree']
  8.  
  9. # Function to add players to teams
  10. def add_player_to_team(team):
  11. player_picked = players.pop()
  12. team.append(player_picked)
  13.  
  14. # count the players available
  15. num_of_players = sum(1 for num_of_players in players)
  16.  
  17. # Test we have an even number of players
  18. if num_of_players % 2 != 0:
  19. # Odd number of players so can' pick teams
  20. print("Can't pick teams as odd number of players !")
  21. elif num_of_players == 0:
  22. print("No players to select from !")
  23. else:
  24. # Shuffle the list of players
  25. shuffle(players)
  26. # Now repeatedly add a player to each team
  27. for player in range(0, num_of_players, 2):
  28. add_player_to_team(teamA)
  29. add_player_to_team(teamB)
  30. print(teamA)
  31. print(teamB)
Add Comment
Please, Sign In to add comment