def generate(size): matrix = [] for row in range(size): matrix.append([]) for col in range(size): (matrix[row]).append(0) return matrix def show(matrix): for row in matrix: for elem in row: print str(elem).rjust(4), print def magic(matrix): size = len(matrix) row = size-1 col = size/2 for k in range(1, size*size+1): matrix[row][col] = k newrow = row + 1 newcol = col + 1 if newrow >= size: newrow = 0 if newcol >= size: newcol = 0 if (matrix[newrow][newcol] != 0) or (row == size-1 and col == size-1): row = row - 1 # col = col else: row = newrow col = newcol # show(matrix) # print "-" * 20 def checkRows(matrix): for row in matrix: print sum(row)