This print_board is the code to reconstruct the the sudoku.
it works perfectly fine when I use the sudoku numbers from the
board, but it doesn’t work from the sudoku.csv file. What have I done wrong?
""" Sudoku.csv =
7;8;0;4;0;0;1;2;0
6;0;0;0;7;5;0;0;9
0;0;0;6;0;1;0;7;8
0;0;7;0;4;0;2;6;0
0;0;1;0;5;0;9;3;0
9;0;4;0;6;0;0;0;5
0;7;0;3;0;0;0;1;2
1;2;0;0;0;7;4;0;0
0;4;9;2;0;6;0;0;7
"""
#these are the sudoku numbers on the sudoku.csv file
import csv
with open('sudoko.csv') as f:
#next(f)
board = csv.reader(f,delimiter=';')
for row in board:
print(row)
#this is the code to read the sudoku.csv file.
def print_board(bo):
for i in range(len(bo)):
if i % 3 == 0 and i != 0:
print("- - - - - - - - - - - - - ")
for j in range(len(bo[0])):
if j % 3 == 0 and j != 0:
print(" | ", end="")
if j == 8:
print(bo[i][j])
else:
print(str(bo[i][j]) + " ", end="")
print(print_board(row))
>Solution :
You can use this:
import csv
with open('sudoko.csv') as f:
board = list(csv.reader(f,delimiter=';'))
def print_board():
for row in range(len(board)):
if row % 3 == 0:
print('|', ('- '*3 + '| ') * 3)
for item in range(len(board[row])):
if item % 3 == 0:
print('| ', end='')
print(board[row][item], end=' ')
print('|')
print('|', ('- '*3 + '| ') * 3)
print_board()
Output:
| - - - | - - - | - - - |
| 7 8 0 | 4 0 0 | 1 2 0 |
| 6 0 0 | 0 7 5 | 0 0 9 |
| 0 0 0 | 6 0 1 | 0 7 8 |
| - - - | - - - | - - - |
| 0 0 7 | 0 4 0 | 2 6 0 |
| 0 0 1 | 0 5 0 | 9 3 0 |
| 9 0 4 | 0 6 0 | 0 0 5 |
| - - - | - - - | - - - |
| 0 7 0 | 3 0 0 | 0 1 2 |
| 1 2 0 | 0 0 7 | 4 0 0 |
| 0 4 9 | 2 0 6 | 0 0 7 |
| - - - | - - - | - - - |