gpt4 book ai didi

Python 3 - 数字标记网格和添加边框?

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

我正在尝试添加数字标签,以及一行和一列来为我的程序组织我的游戏板。我注意到如果我使 emptyGrid = ["OOOO", "OOOO", "OOOO", "OOOO"] 会起作用,但是 sizeof_grid 应该代表用户输入的任何值,所以我不能真正使 emptygrid 相等到设定的网格大小。也就是说,当 emptygrid = [] 时,我无法让程序显示网格标签。当我用我当前的程序执行此操作时,它什么也不打印。

sizeof_grid = 4
emptyGrid = []

def initialize_board():
start_board=[["O" for i in range(sizeof_grid)] for i in
range(sizeof_grid)]
return start_board

def players_board(grid):
for i in range(len(grid)):
print(str(i) + "|" + grid[i])
print(players_board(emptyGrid))

我希望代码输出如下内容:

|01234

0|呜呜呜

1|呜呜呜

2|呜呜呜

3|呜呜呜

4|呜呜呜

(每行之间没有空格)

最佳答案

您的代码中有一些错误。您不调用 initialize_board 函数。此外,players_board 函数中也存在一些错误。

修改后的代码(有变化的行注释并解释变化):

sizeof_grid = 4
emptyGrid = []

def initialize_board():
start_board = [["O" for i in range(sizeof_grid)] for i in
range(sizeof_grid)]
return start_board

def players_board(grid):
print(" |" + "".join([str(x) for x in range(len(grid[0]))])) # print the column numbers
for i in range(len(grid)):
print(str(i) + "|" + "".join(grid[i])) # convert the list to "" joined chars

emptyGrid = initialize_board() # call this to initialize the board
players_board(emptyGrid)

HTH.

关于Python 3 - 数字标记网格和添加边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53531979/

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