Python 如何打印带有坐标的电路板?

Python 如何打印带有坐标的电路板?,python,Python,我有一本字典,上面有一些坐标,每个坐标是真是假。 让我们这样说: {(0, 0): False, (0, 1): False, (1, 0): True, (1, 1): False} 我想做一个def,它取这本字典,如果坐标为false,则打印一个空方块,如果坐标为true,则打印一个填充方块。 到目前为止,我写了以下内容: def printboard(board): sizer = int(get_size(board)) for x in range(sizer):

我有一本字典,上面有一些坐标,每个坐标是真是假。 让我们这样说:

{(0, 0): False, (0, 1): False, (1, 0): True, (1, 1): False}
我想做一个def,它取这本字典,如果坐标为false,则打印一个空方块,如果坐标为true,则打印一个填充方块。 到目前为止,我写了以下内容:

def printboard(board):
    sizer = int(get_size(board))
    for x in range(sizer):
        falseCount = 0
        trueCount = 0
        for y in range(sizer):
            if board[x,y] == False:
                falseCount += 1
            if board[x,y] == True:
                trueCount += 1
        print('⬛'*trueCount + '⬜'*falseCount)   
但是当我编译它时,它不会打印出真正的正方形。 有人知道怎么做吗?
提前谢谢你

您根本不需要计数,您可以使用带有end=的print's:

输出:

⬜⬛
⬜⬜
这将输入正确的换行符,并使用非矩形输入。

给你

x = {
    (0, 0): False, (0, 1): False, 
    (1, 0): True,  (1, 1): False
}

# build a board and fills it with True (⬜)
def build_board(size):
    board = []
    for i in range(size):
        board += [[]]
        for j in range(size):
            board[i] += ['⬜']
    return board 

# fills False entries in the board from the dict (⬛)
def fill_board(data, board):
    for key, value in x.items():
        if value == False:
            board[key[0]][key[1]] = '⬛'
    return board   

# prints the board
def print_board(board):
    for i in range(len(board)):
        for j in range(len(board[i])):
            print(board[i][j], end = "")
        print()

## testing the functions
if __name__ == '__main__':
    board = build_board(2)
    board = fill_board(x, board)
    print_board(board)
输出:

⬛⬛
⬜⬛

什么是get_尺寸?预期输出?如何打印电路板?对于每一行,这将首先打印所有的填充方块,然后打印所有的空方块,而不考虑它们的实际位置。我强烈建议您在线研究其他玩家如何表示简单的游戏区域。典型的方法是使用NumPy数组或嵌套列表,而不是这种dict格式。2D布局更易于索引和操作,几乎可以用于所有目的。为什么在代码中使用falsecount和truecount?最后一行是关于什么的?x y打印'⬛' 如果电路板[x,y]else'⬜', end=False}它被称为三元it打印。。。。。。。。s的东西-什么是打印取决于三元:黑暗块,如果董事会[x,y]是真的,否则白色块
⬛⬛
⬜⬛