Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Three_ways_to_list written by ken Adams
- # Explores the use of list to make teams of players. There are three parts to the program.
- #Each part does something different
- #Part one makes two teams using shuffle and pop
- #Part two makes three teams using sample and modulus
- #Part three makes three teams using sample, modulus and a while loop checks for an empty string
- # code is well annotated and extra print statements show what is happening
- ############################################## imports ########################3
- from random import choice # adds the choice function from the random library
- from random import shuffle
- from random import sample
- #####################################Two teams using shuffle and taling an item off either end of players list###
- print("PART ONE MAKES TWO TEAMS USING SHUFFLE AND POP\n")
- # test data:four list two with an even number of items and two odd
- players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']# 18 players
- #players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q']# 17 players
- #players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P']# 16 players
- #players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O']# 15 players
- #players = ['Harry', 'Ginny']# should only work once
- #players = ['Harry'] # cannot work, in fact teamA and teamB are empty
- teamA =[]
- teamB = []
- shuffle(players)# randomly suffle the list
- print("Printing the shuffled players list.")
- print("shuffled players")
- print(players)
- # as long as there is more than one player in the list we take a player from each side
- while len(players) >1:
- teamA.append(players[0])# take the first one
- teamB.append(players[-1])# take the second one
- players.pop(0)# removing from the players list using pop
- players.pop(-1)# remove the last one
- # players is now two items shorter
- # If there was an odd number of players the middle one does not get to play
- print("Below are players, teamA and teamB. Note: players is now an empty list")
- print(players)
- print(teamA)
- print(teamB)
- ##############################3end of shuffle and pop ###############
- print("\nPART TWO MAKES THREE TEAMS USING SAMPLE AND MODULUS\n")
- # We want three teams of four player from the players2 list
- players2 = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']
- chosen2 = sample(players2,12)
- teams2 = [ [], [], [] ]# sets up an empty list of three empty lists
- for x in range(len(chosen2)):# looping over the chosen2 list
- teams2[x%3].append(chosen2[x])# % is the modulus function it runs 0,1,2,,0,1,2,,etc
- # each item of chosen2 is allocated to one of the teams
- #each sub-list of teams will have 4 members as 12 divides 3 is 4 exactly
- print("We print the items in chosen2")
- print(chosen2)
- print("Now we print teams 0,1, and 2")
- print(teams2[0])
- print(teams2[1])
- print(teams2[2])
- #################################### using a while loop and checking for an empty list #######
- print("\nPART THREE MAKES THREE TEAMS USING SAMPLE, MODULUS AND A WHILE LOOP CHECKS FOR AN EMPTY STRING\n")
- # These are copies of the lists above,
- players3 = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']
- chosen3 = sample(players3,12)# we want three teams of four so we randonly select twelve players
- teams3 = [ [], [], [] ] # empty list of lists
- print("Printing chosen3")
- print(chosen3)
- x = len(chosen3)-1 # x is set to the hightst index in the chosen3 list
- # we continue removong the last item in chosen3 and appending it to a team, stopping when chosen3 is empty
- while len(chosen3) !=0: # an empty list has length zero
- print("x value " +str(x) + " length at chosen at start of while " +str(len(chosen3)))#optional
- teams3[x%3].append(chosen3[x])# % is the modulus function, %3 converts a number to 0, or 1 or 2
- chosen3.remove(chosen3[x])# removes the item from chosen 3, now lwn(chosen3) is one less
- x = x-1 # so we make x one less as well
- print("\nThese are the three teams")
- for x in range(3):
- print(teams3[x])
- #######################################
- print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement