所以我有一个 python 函数,可以显示具有任意数量的行和列的游戏板:
def displayy(rows:int, cols:int)-> None:
for row in range(rows):
print('|' + ' ' * (3*cols) + '|')
print(' ' + '-' * 3* cols)
现在使用这个,如果我有这个作为用户输入:
4 #number of rows
4 #number of cols
CONTENTS
# there are four spaces on this line of input
B HF
CGJB
DBFC
如何将这些字母打印到板上?这样:
| |
| B H F |
| C G J B |
| D B F C |
------------
请您参考如下方法:
除非行和列不相同,否则这应该可行。
# use length of strings as columns
# calculate number of rows based on length of columns
# dispay empty rows at top
def display(board):
empty_rows = len(board[0]) - len(board)
board = [" " * len(board[0]) for _ in range(empty_rows)] + board #add empty rows to the top
for row in board:
print("| " + " ".join(list(row)) + " |")
print(" " + ('-' * 3*len(board)))
board = ["B HF", "CGJB" , "DBFC"]
display(board)
输出:
| |
| B H F |
| C G J B |
| D B F C |
------------
