# [Tutor] Sudoku

Dave Angel d at davea.name
Sun Sep 23 20:46:32 CEST 2012

```On 09/23/2012 09:34 AM, myles broomes wrote:

Did you have a valid reason for making a brand new thread with the same
title and practically the same code?  Probably most readers just decided
you were double-posting, and ignored the new message.  Please use

> Me again, I've been sat here for about an hour now staring at this code: # Sudoku
> # Sudoku is a logic-based number-placement puzzle
> # The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 sub-grids that compose the grid contains all of the digits from 1 to 9import randomdef new_board():
>     """Creates the game board. """
>     board=[]
>     for i in range(9):
>         column=[]
>         for j in range(9):
>             column.append(i)
>         board.append(column)
>     return boarddef display_board(board):

Your email program messed up there, throwing out the newline after the
return statement.

>     """Displays the game board on screen. """
>     for i in range(9):
>         for j in range(9):
>             rand_num=random.randint(1,9)
>             if rand_num not in board[i]:
>                 board[i][j]=rand_num

Why are you doing this stuff inside a function called display_board() ?
Generally, you want each function to do one thing, and to do it
completely.  I think you have three functions here:  1) fill the matrix
with random values  2) blank out any that happen to line up in the same
row or column as another of the same number.  3) print the matrix

>             else:
>                 board[i][j]=' '
>             print(board[i][j],"|",end='')
>         print()# assign the new board to a variable and display it on the screen
> game_board=new_board()
> display_board(game_board)
> I'm cant figure out how to make it so that each column only has (at most) 1 of each number. I've managed to do it fine for the rows but I'm not sure of how I can do it for the columns. I dont want it to seem like I'm being lazy and just getting you guys to do all the work for me so I'm not necceserily asking for a solution, just advice really.
>

Write a triply-nested loop (row, column, rowtest), that compares a
particular cell with all the others in the same column.  Any time you
get a match, blank out one of the cells of the match.

--

DaveA

```