This is the lab assignment implemented with dictionaries: import random def generate(lines, columns): d = {} d['lines'] = lines d['columns'] = columns for line in range(lines): for column in range(columns): d[ line, column ] = random.randrange(10) return d def show(matrix): for i in range(matrix['lines']): for j in range(matrix['columns']): print matrix[i, j], print This is what we typed in the shell while we developed it, and while we discussed how the nested lists implementation relates to it: Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.2 >>> ================================ RESTART ================================ >>> 8 5 7 3 7 8 2 9 8 0 8 2 6 8 3 7 5 7 6 9 >>> ================================ RESTART ================================ >>> 3 2 3 9 0 7 6 0 6 5 6 7 7 6 4 4 2 2 3 4 >>> d = { 'b' : 1, 'a' : 2, 'n' : 1 } >>> d {'a': 2, 'b': 1, 'n': 1} >>> d[' '] = 3 >>> d {'a': 2, ' ': 3, 'b': 1, 'n': 1} >>> d.keys() ['a', ' ', 'b', 'n'] >>> ================================ RESTART ================================ >>> Lines: 3 Columns: 6 Traceback (most recent call last): File "C:/Documents and Settings/dgerman/Desktop/lab.py", line 6, in for line in lines: TypeError: 'int' object is not iterable >>> ================================ RESTART ================================ >>> Lines: 3 Columns: 6 * * * * * * * * * * * * * * * * * * >>> ================================ RESTART ================================ >>> Lines: 3 Columns: 6 * * * * * * * * * * * * * * * * * * >>> ================================ RESTART ================================ >>> Lines: 3 Columns: 6 * * * * * * * * * * * * * * * * * * >>> ================================ RESTART ================================ >>> Lines: 6 Columns: 6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * >>> ================================ RESTART ================================ >>> Lines: 3 Columns: 2 0 4 7 6 8 0 >>> ================================ RESTART ================================ >>> Lines: 6 Columns: 8 2 0 3 7 0 4 4 6 4 2 3 3 1 7 8 2 4 5 3 3 9 0 6 9 6 0 4 4 8 7 5 7 1 0 8 4 3 7 1 4 2 2 0 8 8 4 9 2 >>> ================================ RESTART ================================ >>> Lines: 4 Columns: 5 I am initializing entry 0, 0 Traceback (most recent call last): File "C:/Documents and Settings/dgerman/Desktop/lab.py", line 10, in d[ key ] = random.randrange(10), NameError: name 'd' is not defined >>> ================================ RESTART ================================ >>> Lines: 2 Columns: 3 I am initializing entry 0, 0 I am initializing entry 0, 1 I am initializing entry 0, 2 I am initializing entry 1, 0 I am initializing entry 1, 1 I am initializing entry 1, 2 >>> d {'1, 2': (3,), '1, 1': (0,), '1, 0': (8,), '0, 0': (9,), '0, 1': (6,), '0, 2': (1,)} >>> ================================ RESTART ================================ >>> Lines: 2 Columns: 3 I am initializing entry 0, 0 I am initializing entry 0, 1 I am initializing entry 0, 2 I am initializing entry 1, 0 I am initializing entry 1, 1 I am initializing entry 1, 2 >>> d {'1, 2': 4, '1, 1': 3, '1, 0': 8, '0, 0': 0, '0, 1': 7, '0, 2': 2} >>> d.keys() ['1, 2', '1, 1', '1, 0', '0, 0', '0, 1', '0, 2'] >>> ================================ RESTART ================================ >>> Lines: 2 Columns: 3 I am initializing entry (0, 0) I am initializing entry (0, 1) I am initializing entry (0, 2) I am initializing entry (1, 0) I am initializing entry (1, 1) I am initializing entry (1, 2) >>> d {(0, 1): 1, (1, 2): 3, (0, 0): 3, (1, 1): 7, (1, 0): 9, (0, 2): 4} >>> for i in range(2): for j in range(3): print d[i, j], print 3 1 4 9 7 3 >>> ================================ RESTART ================================ >>> Lines: 3 Columns: 4 >>> d {(0, 1): 2, (1, 2): 1, (0, 0): 0, 'lines': 3, (2, 1): 0, (1, 1): 8, (2, 0): 4, (1, 3): 1, (2, 3): 8, (2, 2): 9, (1, 0): 8, (0, 3): 3, (0, 2): 0, 'columns': 4} >>> for i in range(d['lines']): for j in range(d['columns']): print d[i, j], print 0 2 0 3 8 8 1 1 4 0 9 8 >>> ================================ RESTART ================================ >>> Lines: 3for i in range(d['lines']): for j in range(d['columns']): print d[i, j], print Traceback (most recent call last): File "C:/Documents and Settings/dgerman/Desktop/lab.py", line 3, in lines = int(raw_input("Lines: ")) KeyboardInterrupt >>> >>> >>> >>> 2 2 >>> ================================ RESTART ================================ >>> >>> Lines: 3 Columns: 4 >>> d {(0, 1): 0, (1, 2): 4, (0, 0): 0, 'lines': 3, (2, 1): 0, (1, 1): 8, (2, 0): 8, (1, 3): 4, (2, 3): 6, (2, 2): 3, (1, 0): 1, (0, 3): 3, (0, 2): 4, 'columns': 4} >>> show(d) 0 0 4 3 1 8 4 4 8 0 3 6 >>> ================================ RESTART ================================ >>> >>> a = generate(4, 5) >>> a {(1, 3): 1, (3, 0): 5, (2, 1): 1, (0, 3): 9, (1, 2): 2, (3, 3): 3, (2, 2): 4, (1, 1): 8, 'columns': 5, (3, 2): 8, (0, 0): 7, (0, 4): 7, (1, 4): 3, (2, 3): 8, (1, 0): 5, (0, 1): 7, (3, 1): 1, 'lines': 4, (0, 2): 1, (2, 0): 5, (3, 4): 3, (2, 4): 7} >>> show(a) 7 7 1 9 7 5 8 2 1 3 5 1 4 8 7 5 1 8 3 3 >>> show(generate(4, 2)) 5 3 4 6 6 9 4 6 >>> m = generate(12, 15) >>> show(m) 6 1 2 9 9 8 7 4 3 6 8 9 7 6 9 7 1 8 0 5 0 3 1 7 8 2 6 4 8 4 4 7 6 9 2 3 1 8 2 7 2 2 9 6 7 5 5 8 8 9 4 7 6 8 2 9 8 4 0 1 6 7 3 1 7 7 7 8 7 8 2 5 1 0 7 3 9 8 1 8 1 9 6 3 0 1 5 3 3 9 1 1 1 8 0 8 2 0 9 5 6 2 4 6 1 2 1 2 9 2 1 9 2 1 0 3 2 3 5 5 1 8 8 7 4 3 6 6 8 0 6 1 8 3 7 9 7 8 9 0 4 6 0 7 4 3 8 7 5 1 7 2 4 7 5 3 7 9 5 5 2 7 0 8 2 1 0 8 2 1 7 0 6 3 2 9 8 8 4 3 >>> m = [[0, 2, 1], [5, 4, 3]] >>> m [[0, 2, 1], [5, 4, 3]] >>> m = generate(3, 4) >>> show(m) 7 9 0 4 6 7 4 3 2 2 8 9 >>> m = [[7, 9, 0, 4], [6, 7, 4, 3], [2, 2, 8, 9]] >>> m [[7, 9, 0, 4], [6, 7, 4, 3], [2, 2, 8, 9]] >>> len(m) 3 >>> len(m[0]) 4 >>> m = [] >>> row = [] >>> row.append(7) >>> row [7] >>> row.append(9) >>> row.append(0) >>> row.append(4) >>> row [7, 9, 0, 4] >>> m [] >>> m.append(row) >>> m [[7, 9, 0, 4]] >>> row = [] >>> row.append(6) >>> row.append(7) >>> row.append(4) >>> row.append(3) >>> row [6, 7, 4, 3] >>> m [[7, 9, 0, 4]] >>> m.append(row) >>> m [[7, 9, 0, 4], [6, 7, 4, 3]] >>> row = [] >>> row.append(2) >>> row.append(2) >>> row.append(8) >>> row.append(9) >>> row [2, 2, 8, 9] >>> m [[7, 9, 0, 4], [6, 7, 4, 3]] >>> m.append(row) >>> m [[7, 9, 0, 4], [6, 7, 4, 3], [2, 2, 8, 9]] >>> for line in len(m): for column in len(m[0]): print m[line][column], print Traceback (most recent call last): File "", line 1, in for line in len(m): TypeError: 'int' object is not iterable >>> for line in range(len(m)): for column in range(len(m[0])): print m[line][column], print 7 9 0 4 6 7 4 3 2 2 8 9 >>>