[PyAR2] TypeError: 'int' object is not subscriptable

Daniel Veazey daniel at danielveazey.com
Sun Aug 21 16:04:34 CEST 2011


In my efforts to learn Python, I'm trying to write a program to solve a
Sudoku puzzle. Here is the code I have so far:

## define and populate the board by rows; 0 = unsolved position
row = [0, 1, 2, 3, 4, 5, 6, 7, 8]
row[0] = [5, 6, 0, 0, 4, 1, 0, 0, 8]
row[1] = [1, 0, 0, 0, 0, 9, 0, 7, 0]
row[2] = [0, 8, 0, 5, 0, 2, 3, 0, 0]
row[3] = [3, 0, 9, 0, 2, 0, 0, 5, 0]
row[4] = [0, 7, 0, 3, 0, 5, 0, 9, 0]
row[5] = [0, 5, 0, 0, 7, 0, 6, 0, 1]
row[6] = [0, 0, 2, 1, 0, 3, 0, 6, 0]
row[7] = [0, 3, 0, 2, 0, 0, 0, 0, 9]
row[8] = [6, 0, 0, 4, 5, 0, 0, 8, 3]

## define and populate the columns by referring to the rows
col = [0, 1, 2, 3, 4, 5, 6, 7, 8]
col[0] = [row[0[0]], row[1[0]], row[2[0]], row[3[0]], row[4[0]], row[5[0]],
row[6[0]], row[7[0]], row[8[0]]]
col[1] = [row[0[1]], row[1[1]], row[2[1]], row[3[1]], row[4[1]], row[5[1]],
row[6[1]], row[7[1]], row[8[1]]]
col[2] = [row[0[2]], row[1[2]], row[2[2]], row[3[2]], row[4[2]], row[5[2]],
row[6[2]], row[7[2]], row[8[2]]]
col[3] = [row[0[3]], row[1[3]], row[2[3]], row[3[3]], row[4[3]], row[5[3]],
row[6[3]], row[7[3]], row[8[3]]]
col[4] = [row[0[4]], row[1[4]], row[2[4]], row[3[4]], row[4[4]], row[5[4]],
row[6[4]], row[7[4]], row[8[4]]]
col[5] = [row[0[5]], row[1[5]], row[2[5]], row[3[5]], row[4[5]], row[5[5]],
row[6[5]], row[7[5]], row[8[5]]]
col[6] = [row[0[6]], row[1[6]], row[2[6]], row[3[6]], row[4[6]], row[5[6]],
row[6[6]], row[7[6]], row[8[6]]]
col[7] = [row[0[7]], row[1[7]], row[2[7]], row[3[7]], row[4[7]], row[5[7]],
row[6[7]], row[7[7]], row[8[7]]]
col[8] = [row[0[8]], row[1[8]], row[2[8]], row[3[8]], row[4[8]], row[5[8]],
row[6[8]], row[7[8]], row[8[8]]]

When I run the program, the first part of defining the rows runs fine, but
when I start to define the columns, I get this error:

Traceback (most recent call last):
  File "sudoku_solver_1.py", line 15, in <module>
    col[0] = [row[0[0]], row[1[0]], row[2[0]], row[3[0]], row[4[0]],
row[5[0]], row[6[0]], row[7[0]], row[8[0]]]
TypeError: 'int' object is not subscriptable

I'm not quite sure what I'm doing wrong here. I want to nest a list inside
the list col by referring to the nested lists in row. Any tips on why I'm
getting this error or what I should do to fix it?

Thanks,
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/pyar2/attachments/20110821/ee260b27/attachment.html>


More information about the PyAR2 mailing list