Greetings, I'm having a problem with the following test. I make a dictionary with 19 keys (1 to 19). Each key has a list of 7 numbers (A to G)
# Set up the table # key# A B C D E F G tablE= {1:[ 0, 2, 0, 0, 0, 0, 0], # 1 2:[ 1, 3, 3, 0, 0, 0, 0], # 2 3:[ 2, 0, 5, 2, 0, 0, 0], # 3 4:[ 0, 5, 0, 0, 0, 0, 0], # 4 5:[ 4, 0, 0, 3,15,13, 0], # 5 6:[ 0, 0, 1, 0, 0, 0, 0], # 6 7:[ 0, 8, 0, 0, 0, 0, 0], # 7 8:[ 7,10, 0, 0, 0, 0, 0], # 8 9:[ 0,19, 0, 0, 0, 8, 0], # 9 10:[ 8, 0,11, 0, 0, 0, 0], # 10 11:[ 0, 0,10, 0, 0, 0, 0], # 11 12:[ 0, 0, 0,13, 0, 0, 0], # 12 13:[ 0, 0,12, 0, 5, 0, 0], # 13 14:[ 0,15,17, 0, 0, 0, 0], # 14 15:[14, 0, 0, 0, 0, 5, 0], # 15 16:[17, 0,19, 0, 0, 0, 0], # 16 17:[18,16, 0,14, 0, 0, 0], # 17 18:[ 0,17, 0, 0, 0, 0, 0], # 18 19:[ 9, 0, 0,16, 0, 0, 0]} # 19 # key# A B C D E F G The first loop is supposed to populate G with a random range of 4 integers 10 to 109 in random keys 1-19 that have a zero (except keY 6 and keY 11) So keY 6 and keY 11 should both have a zero in G after the four integers have been sown. # populate G column with range of 4 integers 10 to 109 # in random keys that have a zero [except keYs 6 and 11] print "%"*69 cnt=0 while cnt <= 3: print "CNT111=",cnt #debug-remove when done a = range(1,20) # 1 to 19 keY = random.choice(a) if keY == 6 or keY == 11 or tablE.values()[keY-1][6] != 0: tablE.values()[5][6] = 0 tablE.values()[10][6] = 0 cnt -= 1 keY = random.choice(a) if keY != 6 or keY != 11 or table.values()[keY-1][6] == 0: b = range(10,110) # 10 to 109 integer = random.choice(b) tablE.values()[keY-1][6] = integer cnt += 1 The second loop is supposed to populate G with numbers -4 to -1 in random keys 1-19 that have a zero (except keY 6 and keY11). So once again, 6 and 11 should have a zero in G after the loop is finished. # populate G with range of integers -1 to -4 # in random keYs that have a zero [except keYs 6 and 11] cnt=4 while cnt > 0: print "CNT222=",cnt a = range(1,20) if keY != 6 or keY != 11 and tablE.values()[keY-1][6] == 0: keY = random.choice(a) tablE.values()[keY-1][6] = -cnt cnt -= 1 if keY == 6 or keY == 11: tablE.values()[5][6] = 0 tablE.values()[10][6] = 0 cnt += 1 The last thing is that two integers are placed in specific keys 4 and 16, overwriting anything that may be in G whether a negative number or a number > 9. # Put an integer in G at two specific keys: 4 & 16 # These will overwrite anything placed there previously #a = range(1,99) #tablE.values()[3][6]= 100 + random.choice(a) #tablE.values()[15][6]= 100 + random.choice(a) The above has been commented out so I see if the two loops are each distributing four numbers each, without putting anything in G of keys 6 and 11. I've approached the problem by trying to get the loop to repeat if a number ends up in G at key 6 or key 11. I've done this changing the loop count. This seems to work about 97% of the time, or so. I'm looking for 100%. I know I can always just set those keys to zero before the table is written, but I'd rather have the table as fully populated by the two loops and just not have anything get in G in keys 6 & 11. Anyway, this is just a short test, part of a larger program. Here's the test code: #!/usr/bin/python import random print "\n"*30 # Set up the table # key# A B C D E F G tablE= {1:[ 0, 2, 0, 0, 0, 0, 0], # 1 2:[ 1, 3, 3, 0, 0, 0, 0], # 2 3:[ 2, 0, 5, 2, 0, 0, 0], # 3 4:[ 0, 5, 0, 0, 0, 0, 0], # 4 5:[ 4, 0, 0, 3,15,13, 0], # 5 6:[ 0, 0, 1, 0, 0, 0, 0], # 6 7:[ 0, 8, 0, 0, 0, 0, 0], # 7 8:[ 7,10, 0, 0, 0, 0, 0], # 8 9:[ 0,19, 0, 0, 0, 8, 0], # 9 10:[ 8, 0,11, 0, 0, 0, 0], # 10 11:[ 0, 0,10, 0, 0, 0, 0], # 11 12:[ 0, 0, 0,13, 0, 0, 0], # 12 13:[ 0, 0,12, 0, 5, 0, 0], # 13 14:[ 0,15,17, 0, 0, 0, 0], # 14 15:[14, 0, 0, 0, 0, 5, 0], # 15 16:[17, 0,19, 0, 0, 0, 0], # 16 17:[18,16, 0,14, 0, 0, 0], # 17 18:[ 0,17, 0, 0, 0, 0, 0], # 18 19:[ 9, 0, 0,16, 0, 0, 0]} # 19 # key# A B C D E F G # populate G column with range of 4 integers 10 to 109 # in random keys that have a zero [except keYs 6 and 11] print "%"*69 cnt=0 while cnt <= 3: print "CNT111=",cnt a = range(1,20) # 1 to 19 keY = random.choice(a) if keY == 6 or keY == 11 or tablE.values()[keY-1][6] != 0: tablE.values()[5][6] = 0 tablE.values()[10][6] = 0 cnt -= 1 keY = random.choice(a) if keY != 6 or keY != 11 or table.values()[keY-1][6] == 0: b = range(10,110) # 10 to 109 integer = random.choice(b) tablE.values()[keY-1][6] = integer cnt += 1 # populate G with range of integers -1 to -4 # in random keYs that have a zero [except keYs 6 and 11] cnt=4 while cnt > 0: print "CNT222=",cnt a = range(1,20) if keY != 6 or keY != 11 and tablE.values()[keY-1][6] == 0: keY = random.choice(a) tablE.values()[keY-1][6] = -cnt cnt -= 1 if keY == 6 or keY == 11: tablE.values()[5][6] = 0 tablE.values()[10][6] = 0 cnt += 1 # Put an integer in G at two specific keys: 4 & 16 # These will overwrite anything placed there previously #a = range(1,99) #tablE.values()[3][6]= 100 + random.choice(a) #tablE.values()[15][6]= 100 + random.choice(a) # print-out tablE print " data table" print "---------------------" print " values" print "key A B C D E F G" for roomNum in range(0,19): print " ",roomNum+1, for compass in range(0,7): xVal = tablE.values()[roomNum][compass] print xVal, if compass == 6: print I'm looking for suggestions on how to improve the two loops so that I get 100% distribution each time (eight numbers total). I'm running a GNU/Linux OS, Python 2.4.3 -- b h a a l u u at g m a i l dot c o m "You assist an evil system most effectively by obeying its orders and decrees. An evil system never deserves such allegiance. Allegiance to it means partaking of the evil. A good person will resist an evil system with his or her whole soul." [Mahatma Gandhi] _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor