Advertisement
farrismp

2.4 Repeating for all players

Apr 30th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. from random import choice
  2.  
  3. teamA = []
  4. teamB = [] # Create two empty lists for teams A and B
  5.  
  6. players = ['Harry', 'Hermione', 'Neville', 'Ginny']
  7.  
  8. # Function to add players to teams
  9. def add_player_to_team(team):
  10. player_picked = choice(players)
  11. team.append(player_picked)
  12. players.remove(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. # Add a player to each team
  25. for player in range(0, num_of_players, 2):
  26. add_player_to_team(teamA)
  27. add_player_to_team(teamB)
  28. print("Team A is - {}".format(teamA))
  29. print("Team B is - {}".format(teamB))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement