【发布时间】:2019-09-16 09:32:38
【问题描述】:
我想用 for 循环打印字典 self.board,但我不知道怎么做。
我要打印的内容: enter image description here
这是我的代码:
class board:
def __init__(self, length=3):
v = '-'
self.length = length
self.size = self.length ** 2 + 1
self.board = {k: v for k in range(1, self.size)}
def check_board(self):
print(self.board)
def display_board(self):
print("\nDisplay the board:")
for i in range(1, self.length + 1):
for k in range(1, self.length+1): # from here i dont know how to do the right thing to
# display like in the picture
print(self.board[k] + " | ")
【问题讨论】:
-
现在 wrt/ 问题本身:如果你的“板”应该是一个网格,你的数据结构应该以这种或另一种方式反映(这里想到一个列表列表.. .)。作为一般规则:正确的数据结构是简单、容易、明显的代码的关键。
标签: python python-3.x class