如何用坐标打印板?

时间:2018-12-13 18:00:31

标签: python

我有一个带有一些坐标的字典,每个坐标为True或False。 让我们说一个:

{(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)   

但是当我编译它时,它不会打印出真正的正方形。 有谁知道如何使这项工作? 先感谢您!

2 个答案:

答案 0 :(得分:0)

您根本不需要计数,可以将printend=""一起使用:

def printboard(board):
    mx = max(x for x,_ in board.keys() )
    my = max(y for _,y in board.keys() )

    for y in range(my+1):
        for x in range(mx+1): 
            print('⬛' if board[(x,y)] else '⬜', end= "") 
        print("") 

#           x  y
print('⬛' if board[(x,y)] else '⬜', end= "") False})

输出:

⬜⬛
⬜⬜

这将插入正确的换行符,并与非矩形输入配合使用。

答案 1 :(得分:0)

你在这里

$ a2dismod php7.0
$ a2enmod php7.2
$ service apache2 reload

输出:

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)