Suppose I have a dictionary of the form:
the_board = {(1,1) : ' ', (1,2) : ' ', (1,3) : ' ',
(2,1) : ' ', (2,2) : ' ', (2,3) : ' ',
(3,1) : ' ', (3,2) : ' ', (3,3) : ' ',}
I want to print each row line by line. Currently I do something like this:
def display(board):
var = list(board.values()) # Iterator to print out the table
i = 0
j = 0
maxi = len(var)
while i < maxi:
while j < (i + 3):
print(var[j], end="")
if j < i+2:
print('|', end='')
j += 1
print()
if i < (maxi-1):
print("-+-+-")
i += 3
I am aware that this is most likely not the most "pythonic way" to achieve what I want. How would I do so in a more pythonic manner? (I am aware that I can use the keys to achieve this since I gave them coordinate keys, but I may need to print a tabular dictionary without ordered/subscripted keys, so I would appreciate a more general solution).
Found out about Python's range function, so now my code looks like this:
def display(board):
var = list(board.values()) # Iterator to print out the table
maxi = len(var)
for i in range(0, maxi, 3):
for j in range(i, (i+3)):
print(var[j], end="")
if j < i+2:
print('|', end='')
print()
if i < (maxi-1):
print("-+-+-")
Still not sure it's the best way to write it.