On Sun, Sep 23, 2012 at 12:08 PM, myles broomes <[email protected]>wrote:
> > I'm currently coding a Sudoku clone but I'm having trouble displaying the > board: > > # 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 9 > import random > def 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 board > def display_board(board): > """Display game board on screen.""" > for i in range(9): > for j in range(9): > rand_num=random.randint(0,9) > if rand_num not in board[i]: > board[i][j]=rand_num > else: > board[i][j]=' ' > print(board[i][j],"|") > # assign the new board to a variable and display it on the screen > game_board=new_board() > display_board(game_board) > > I cant think of a way to get each column in the for loop (board[i]) to > print side by side. Any help would be much appreciated. > > Which version of python are you using? In python 2.x, you can prevent print from adding a newline at the end by appending a comma, like this: print board[i][j], "|", in python 3.x, the print function takes an "end" argument which does the same thing: # default for end is "\n", but here we force no end character print(board[i][j], "|", end="") this way you can print multiple things on a single line. Now the only other thing you need to do is add a single newline after you print a single row. You can do that with just an empty print() call (or print statement in python 2.x) HTH, Hugo
_______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
