Advertisement
GCK

GCK/ team_maker function with shuffle

GCK
Sep 25th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. import random
  2. from random import choice, randint, shuffle
  3.  
  4.  
  5.  
  6. print("Hello I'm gigi your bot, let's play a team maker game")
  7. team1= []
  8. team2= []
  9. players=['Harry','Hermione','Neville','Ginny']
  10. reserve_list=[]
  11. total_number_of_players=len(players)
  12. half_team=len(players)/2
  13. print("My list has "+ str(total_number_of_players) + " players")
  14.  
  15. """players_parity counts the number of players in the list and if uneven select a reserve player who
  16. will not be yet in a team
  17. """
  18. def players_parity():
  19.    
  20.     if total_number_of_players % 2==0:
  21.         print("all players will play")
  22.     else:
  23.         print("Number of players is uneven, one is randomly added to the reserve list and will play later")
  24.         reserve_player=choice(players)
  25.         reserve_list.append(reserve_player)
  26.         players.remove(reserve_player)
  27.     return players,len(reserve_list)
  28.          
  29.  
  30. """add_player_randomlyshuffled_to_team(team) will add one player after shuffling and pop the last
  31. player from the list and add that player to a team
  32. """      
  33. def add_player_randomlyshuffled_to_team(team):
  34.     shuffle(players)
  35.     team.append(players[-1])
  36.     players.pop()
  37.    
  38.     return team,players
  39.  
  40.  
  41. """add_player_randomlyshuffled_to_team(team) will add one player after shuffling and pop the last
  42. player from the list and add that player to a team
  43. """
  44. def add_player_randomlyshuffled_to_random_team():
  45.     random_team=randint(0,1)
  46.  
  47.     while  len(players)!=0:
  48.         if random_team==0 and len(team1)!=half_team:
  49.            add_player_randomlyshuffled_to_team(team1)
  50.         elif random_team==1 and len(team2)!=half_team:
  51.            add_player_randomlyshuffled_to_team(team2)
  52.         elif random_team==0 and len(team1)==half_team:
  53.            add_player_randomlyshuffled_to_team(team2)
  54.         elif random_team==1 and len(team2)==half_team:
  55.            add_player_randomlyshuffled_to_team(team1)
  56.     return team1, team2, len(players)
  57.  
  58.  
  59.  
  60. players,reserve_list_size=players_parity()
  61. print(players)
  62. team1, team2, players_left_size=add_player_randomlyshuffled_to_random_team()
  63. print("Team 1 is "+ str(team1))
  64. print("Team 2 is "+ str(team2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement