0

I'm trying to set this in Python. I think that I am close but not quite sure where to go with it. I want each section of list to fill in each row of Sudoku layout, but I can only get it to fill in the whole list in each section. Could I get some advice on a direction to take?

list = [[5,0,0,0,0,0,0,0,0],[0,9,0,7,0,0,8,0,0],[0,0,0,0,3,0,0,7,0],[6,0,1,0,0,0,9,8,0],[0,0,0,6,0,0,0,0,0],[0,0,9,0,0,0,7,0,1],[0,0,0,0,0,8,1,9,0],[0,4,0,5,0,1,0,0,8],[0,7,0,3,0,6,0,4,0]]

for c in range(3): 
    if c == 0 or 2 or 8:
        print('+-------+-------+-------+')
    for x in list:  
        row = '| '       
        for y in range(0,9):
            if x[y] == 0:
                x[y] = '.'
                row += x[y] + ' '
            else:
                row += str(x[y]) + ' '   
            
            if y == 2:
                 row += '| '
            if y == 5:
                row += '| '
            if y == 8:
                row += '| '
                    
       # if y == 3 or 7 or 8: 
        #    row += '| '
        print(row)
        row = '\n' 
print('+-------+-------+-------+')

I haven't tried too much as most of what I do messes up the layout of the grid, but I have tried moving the start and end point of the for loops and moving it to where the print(row) sits but I end up with really off-the-wall results; i.e., I get nothing or the bar that is supposed to concatenate in prints every other line.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251

1 Answers1

0

if c == 0 or 2 or 8: is incorrect. A non-zero value is "truthy" so you are saying if c == 0 or True or True: which is always true. Use if c==0 or c==2 or c==8: or if c in (0, 2, 8): (really should be 0,3,6).

Use enumerate() which returns an index and data to simplify looping. Override end in print to output as needed instead of concatenating a whole row at once.

list is also a bad name for a variable as it shadows the built-in list type.

board = [[5, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 9, 0, 7, 0, 0, 8, 0, 0],
         [0, 0, 0, 0, 3, 0, 0, 7, 0],
         [6, 0, 1, 0, 0, 0, 9, 8, 0],
         [0, 0, 0, 6, 0, 0, 0, 0, 0],
         [0, 0, 9, 0, 0, 0, 7, 0, 1],
         [0, 0, 0, 0, 0, 8, 1, 9, 0],
         [0, 4, 0, 5, 0, 1, 0, 0, 8],
         [0, 7, 0, 3, 0, 6, 0, 4, 0]]

for row, rowdata in enumerate(board):
    if row in (0, 3, 6):
        print('+-------+-------+-------+')
    for col, value in enumerate(rowdata):
        if col in (0, 3, 6):
            print('|', end=' ')
        if value == 0:
            print('.', end=' ')
        else:
            print(value, end=' ')
    print('|')
print('+-------+-------+-------+')

Output:

+-------+-------+-------+
| 5 . . | . . . | . . . |
| . 9 . | 7 . . | 8 . . |
| . . . | . 3 . | . 7 . |
+-------+-------+-------+
| 6 . 1 | . . . | 9 8 . |
| . . . | 6 . . | . . . |
| . . 9 | . . . | 7 . 1 |
+-------+-------+-------+
| . . . | . . 8 | 1 9 . |
| . 4 . | 5 . 1 | . . 8 |
| . 7 . | 3 . 6 | . 4 . |
+-------+-------+-------+
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251