Reputation: 172
I am making a Tic Tac Toe program in Python 3.4.1. I have code as follows:
def boardDraw():
board = 0,0,0,\
0,0,0,\
0,0,0
print(board[0] , "|" , board[1] , "|" , board[2], \
"\n----------\n" , \
board[3] , "|" , board[4] , "|" , board[5], \
"\n----------\n" , \
board[6] , "|" , board[7] , "|" , board[8])
boardDraw()
Output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Desired output:
0 | 0 | 0
----------
0 | 0 | 0
----------
0 | 0 | 0
Is the only way to have my desired output is inserting the following:
print(end = " ")
In between board and my current print statement? I would like to have it within one print, if possible.
Upvotes: 0
Views: 242
Reputation: 1
Here if you want is how I did it before to display the tic-tac-toe board:
def __init__(self):
self.game_list = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
def print_game_board(self):
for list in self.game_list:
print(" | ".join(list))
print("--------")
Upvotes: 0
Reputation: 47652
There is the quick and dirty method that user2864740 provided that will solve the problem. It was my first thought when I saw the code as it was:
print(board[0] , "|" , board[1] , "|" , board[2], \
to:
print("", board[0] , "|" , board[1] , "|" , board[2], \
I really don't recommend this way of formatting it becomes very hard to read. This can be made a bit easier to read and maintain. You can use Python's formatting and then provide the data as parameters using %
.
So I'd use something like:
boardsep = "-" * 10
boardline = "%s | %s | %s\n%s"
print(boardline % (board[0], board[1], board[2], boardsep))
print(boardline % (board[3], board[4], board[5], boardsep))
print(boardline % (board[6], board[7], board[8], boardsep))
The boardsep
is just a convenient way of taking what's in the string and duplicating it a number of times (in this case 10 times). Since the way you print the boardline
out is the same for each line I would assign it to a variable so it can be reused. You can read these print formatting docs to get a better understanding of how the parameters and format string work together.
mgilson also proposed a good solution (I upvoted it) and had me look at the OP's question again. It was for Python3 there are things you can do such as the new format method on strings, slicing and expansion. The
boardsep = '-' * 10
boardline = '{0:^3}|{1:^3}|{2:^3}\n{sep}''
print (boardline.format(*board[0:3], sep=boardsep))
print (boardline.format(*board[3:6], sep=boardsep))
print (boardline.format(*board[6:9], sep=boardsep))
But you can go one further and reduce it to one complex line. If you get a thorough understanding of the basics above one could try this:
print ((('{:^3}|{:^3}|{:^3}\n'+('-'*10)+'\n') * 3).format(*board))
If you were to print out the expanded format specifier that generates the board it would look like this:
{:^3}|{:^3}|{:^3}\n----------\n{:^3}|{:^3}|{:^3}\n----------\n{:^3}|{:^3}|{:^3}\n----------\n
Since the OP did notice the problems in the output I will provide one last edit for code that is a bit more dynamic and could be put into an expanded function to generate the boards.
linesepchar = '-'
colsepchar = '|'
numrows = 3
numcols = 3
fieldwidth = 3
linesep = '\n{linesepchar:{linesepchar}^{linewidth}}\n'
fieldspec = '{:^{fieldwidth}}'
lineformat = (fieldspec+'{colsepchar}')*(numcols-1)+fieldspec
boardstr = (((lineformat+linesep)*(numrows-1)+lineformat).format( \
*board,linesepchar=linesepchar, colsepchar=colsepchar, \
fieldwidth=fieldwidth, linewidth=((fieldwidth+1)*numcols-1)))
Upvotes: 3
Reputation: 310297
Why not use .format
?
print ('{0:^3}|{1:^3}|{2:^3}'.format(*board))
Here the specifier: ^3
means to make a field of width 3 and center the text if possible.
Upvotes: 5