gpt4 book ai didi

python - 如何让输入的字母显示在游戏板上?

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:20 25 4
gpt4 key购买 nike

所以我有一个 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 |
------------

关于python - 如何让输入的字母显示在游戏板上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com