Question: In Python 3: Modify the following Sudoku puzzle program so that the Sudoku puzzle table is filled with the values that are stored in the
In Python 3:
Modify the following Sudoku puzzle program so that the Sudoku puzzle table is filled with the values that are stored in the attached file (see below for txt file contents). Be sure to use try/except to handle errors in the event of a file input error.
(Text file contents are as follows:)
0 3 5 2 9 0 8 6 4 0 8 2 4 1 0 7 0 3 7 6 4 3 8 0 0 9 0 2 1 8 7 3 9 0 4 0 0 0 0 8 0 4 2 3 0 0 4 3 0 5 2 9 7 0 4 0 6 5 7 1 0 0 9 3 5 9 0 2 8 4 1 7 8 0 0 9 0 0 5 2 6
(End of text file)
def chkRow(puzzle, val, row): if val in puzzle[row-1]: return False; else: return True; def chkCol(puzzle, val, col): for i in range(0, len(puzzle)): if val == puzzle[i][col-1]: return False; else: return True; def isValid(puzzle): loop = True; while loop: row = int(input(" Enter row: ")) col = int(input(" Enter column: ")) val = int(input(" Enter Value: ")) if(chkRow(puzzle, val, row) and chkCol(puzzle, val, col)): loop = False; else: print(" Invalid value... "); print(" Correct Value "); puzzle[row-1][col-1] = val; display_sudoku(puzzle); def isFull(puzzle): for row in puzzle: for col in row: if col == 0: return False; return True; def display_sudoku(puzzle): for row in puzzle: for col in row: if col == 0: print(' ', end=" ") else: print(col, end=" ") print() if __name__ == '__main__': puzzle = [[0, 8, 0, 5, 0, 2, 4, 0, 0], [0, 0, 9, 0, 0, 1, 0, 0, 0], [1, 3, 4, 0, 8, 0, 0, 0, 0], [0, 9, 0, 1, 0, 0, 0, 0, 0], [3, 0, 2, 0, 0, 0, 7, 0, 1], [0, 0, 0, 0, 0, 3, 0, 5, 0], [0, 0, 0, 0, 1, 0, 2, 7, 4], [0, 0, 0, 3, 0, 0, 9, 0, 0], [0, 0, 5, 6, 0, 7, 0, 3, 0]] display_sudoku(puzzle) while not isFull(puzzle): isValid(puzzle)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
