Advertisement
kenadams53

Three_ways_to_list

Aug 25th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. # Three_ways_to_list written by ken Adams
  2. # Explores the use of list to make teams of players. There are three parts to the program.
  3. #Each part does something different
  4. #Part one makes two teams using shuffle and pop
  5. #Part two makes three teams using sample and modulus
  6. #Part three makes three teams using sample, modulus and a while loop checks for an empty string
  7. # code is well annotated and extra print statements show what is happening
  8.  
  9. ############################################## imports ########################3
  10. from random import choice # adds the choice function from the random library
  11. from random import shuffle
  12. from random import sample
  13.  
  14. #####################################Two teams using shuffle and taling an item off either end of players list###
  15. print("PART ONE MAKES TWO TEAMS USING SHUFFLE AND POP\n")
  16. # test data:four list two with an even number of items and two odd
  17. players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']# 18 players
  18. #players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q']# 17 players
  19. #players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P']# 16 players
  20. #players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O']# 15 players
  21. #players = ['Harry', 'Ginny']# should only work once
  22. #players = ['Harry'] # cannot work, in fact teamA and teamB are empty
  23. teamA =[]
  24. teamB = []
  25. shuffle(players)# randomly suffle the list
  26. print("Printing the shuffled players list.")
  27. print("shuffled players")
  28. print(players)
  29. # as long as there is more than one player in the list we take a player from each side
  30. while len(players) >1:
  31. teamA.append(players[0])# take the first one
  32. teamB.append(players[-1])# take the second one
  33. players.pop(0)# removing from the players list using pop
  34. players.pop(-1)# remove the last one
  35. # players is now two items shorter
  36. # If there was an odd number of players the middle one does not get to play
  37.  
  38. print("Below are players, teamA and teamB. Note: players is now an empty list")
  39. print(players)
  40. print(teamA)
  41. print(teamB)
  42. ##############################3end of shuffle and pop ###############
  43. print("\nPART TWO MAKES THREE TEAMS USING SAMPLE AND MODULUS\n")
  44. # We want three teams of four player from the players2 list
  45. players2 = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']
  46. chosen2 = sample(players2,12)
  47. teams2 = [ [], [], [] ]# sets up an empty list of three empty lists
  48.  
  49. for x in range(len(chosen2)):# looping over the chosen2 list
  50. teams2[x%3].append(chosen2[x])# % is the modulus function it runs 0,1,2,,0,1,2,,etc
  51. # each item of chosen2 is allocated to one of the teams
  52. #each sub-list of teams will have 4 members as 12 divides 3 is 4 exactly
  53. print("We print the items in chosen2")
  54. print(chosen2)
  55. print("Now we print teams 0,1, and 2")
  56. print(teams2[0])
  57. print(teams2[1])
  58. print(teams2[2])
  59. #################################### using a while loop and checking for an empty list #######
  60. print("\nPART THREE MAKES THREE TEAMS USING SAMPLE, MODULUS AND A WHILE LOOP CHECKS FOR AN EMPTY STRING\n")
  61. # These are copies of the lists above,
  62. players3 = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']
  63. chosen3 = sample(players3,12)# we want three teams of four so we randonly select twelve players
  64. teams3 = [ [], [], [] ] # empty list of lists
  65. print("Printing chosen3")
  66. print(chosen3)
  67.  
  68. x = len(chosen3)-1 # x is set to the hightst index in the chosen3 list
  69. # we continue removong the last item in chosen3 and appending it to a team, stopping when chosen3 is empty
  70. while len(chosen3) !=0: # an empty list has length zero
  71. print("x value " +str(x) + " length at chosen at start of while " +str(len(chosen3)))#optional
  72. teams3[x%3].append(chosen3[x])# % is the modulus function, %3 converts a number to 0, or 1 or 2
  73. chosen3.remove(chosen3[x])# removes the item from chosen 3, now lwn(chosen3) is one less
  74. x = x-1 # so we make x one less as well
  75. print("\nThese are the three teams")
  76. for x in range(3):
  77. print(teams3[x])
  78.  
  79. #######################################
  80.  
  81. print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement