Python 如何在游戏板上显示输入字母?
因此,我有一个python函数,可以显示具有任意行数和列数的游戏板:Python 如何在游戏板上显示输入字母?,python,python-3.x,Python,Python 3.x,因此,我有一个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
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 |
------------
| |
| 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)
输出: