I have to update a nested list sudoku with grids and borders. My output is not printing as intended and I am not sure how to fix it. This is my code with the test input cases and intended output vs output right now.
The code below has the following:
This is the description:
Prints a given board to the console in a way that aligns the content of columns and makes the subgrids visible.
Input : a Sudoku board (board) of size 4x4, 9x9, or 16x16
Effect: prints the board to the console
small = [[0, 0, 1, 0],
[4, 0, 0, 0],
[0, 0, 0, 2],
[0, 3, 0, 0]]
big = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
[4, 0, 0, 7, 8, 9, 0, 0, 0],
[7, 8, 0, 0, 0, 0, 0, 5, 6],
[0, 2, 0, 3, 6, 0, 8, 0, 0],
[0, 0, 5, 0, 0, 7, 0, 1, 0],
[8, 0, 0, 2, 0, 0, 0, 0, 5],
[0, 0, 1, 6, 4, 0, 9, 7, 0],
[0, 0, 0, 9, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 0, 0, 0, 2]]
giant = [[0, 5, 0, 0, 0, 4, 0, 8, 0, 6, 0, 0, 0, 0, 9, 16],
[1, 0, 0, 0, 0, 0, 0, 13, 4, 0, 0, 7, 15, 0, 8, 0],
[13, 0, 0, 0, 0, 7, 3, 0, 0, 0, 0, 9, 5, 10, 0, 0],
[0, 11, 12, 15, 10, 0, 0, 0, 0, 0, 5, 0, 3, 4, 0, 13],
[15, 0, 1, 3, 0, 0, 7, 2, 0, 0, 0, 0, 0, 5, 0, 0],
[0, 0, 0, 12, 0, 3, 0, 5, 0, 11, 0, 14, 0, 0, 0, 9],
[4, 7, 0, 0, 0, 0, 0, 0, 12, 0, 15, 16, 0, 0, 0, 0],
[0, 0, 0, 0, 14, 0, 15, 0, 6, 9, 0, 0, 0, 0, 12, 0],
[3, 0, 15, 4, 0, 13, 14, 0, 0, 0, 0, 1, 0, 0, 7, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0],
[11, 0, 16, 10, 0, 0, 0, 0, 0, 7, 0, 0, 0, 3, 5, 0],
[0, 0, 13, 0, 0, 0, 0, 0, 14, 0, 15, 16, 0, 9, 0, 1],
[9, 0, 2, 0, 0, 14, 0, 4, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 14, 0, 0, 0, 0, 0, 10, 9, 0, 3, 0, 0, 0, 1, 7],
[8, 0, 0, 0, 16, 0, 0, 1, 2, 14, 11, 4, 0, 0, 0, 3],
[0, 0, 0, 1, 0, 0, 5, 0, 0, 16, 0, 6, 0, 12, 0, 0]]
import math
def print_board(board):
board_size = len(board)
l = int(board_size + math.sqrt(board_size) + 1)
section = int(math.sqrt(board_size))
border = 0
i_index = 0
for i in range(0, l):
if (i == border):
print('-' * l)
border = border + section + 1
else:
inner_border = 0
j_index = 0
for j in range(0, l):
if (j == inner_border):
print('|', end='')
inner_border = inner_border + section + 1
else:
if (board[i_index][j_index] == 0):
print(' ', end='')
else:
if (board[i_index][j_index] < 10):
print(board[i_index][j_index], end='')
if (board[i_index][j_index] == 10):
print('A', end='')
if (board[i_index][j_index] == 11):
print('B', end='')
if (board[i_index][j_index] == 12):
print('C', end='')
if (board[i_index][j_index] == 13):
print('D', end='')
if (board[i_index][j_index] == 14):
print('E', end='')
if (board[i_index][j_index] == 15):
print('F', end='')
if (board[i_index][j_index] == 16):
print('G', end='')
j_index = j_index + 1
i_index = i_index + 1
print(print_board(small))
print(print_board(big))
print(print_board(giant))
Intended output:
-------
| |1 |
|4 | |
-------
| | 2|
| 3| |
-------
>>> print_board(big)
-------------
| | | |
|4 |789| |
|78 | | 56|
-------------
| 2 |36 |8 |
| 5| 7| 1 |
|8 |2 | 5|
-------------
| 1|64 |97 |
| |9 | |
| | 3 | 2|
-------------
>>> print_board(giant2)
---------------------
| 5 | 4 8| 6 | 9G|
|1 | D|4 7|F 8 |
|D | 73 | 9|5A |
| BCF|A | 5 |34 D|
---------------------
|F 13| 72| | 5 |
| C| 3 5| B E| 9|
|47 | |C FG| |
| |E F |69 | C |
---------------------
|3 F4| DE | 1| 78|
| | | 9A| |
|B GA| | 7 | 35 |
| D | |E GF| 9 1|
---------------------
|9 2 | E 4|8 | |
| E | A|9 3 | 17|
|8 |G 1|2EB4| 3|
| 1| 5 | G 6| C |
---------------------
My output:
You need to print a newline character between each row, easiest way to do it is to use print()
when you want to create a new line.
try this:
# ...
i_index = i_index + 1
print()
# ...
When you increase the i_index
parameter you are basically going over to the next line, so this is where you want your print()
statement as well
Also, since you are using print
inside the function print_board()
, you don't need to use print(print_board(big))
for example, you should just use print_board(big)
. that is why you are seeing None
printed between each board (because print_board()
returns None)