python - 如何用Python制作动态网格

标签 python grid

我正在构建一个 X 和 O(井字棋)应用程序,用户可以决定网格是否在 5X5 和 10X10 之间,如何编写代码以使网格是动态的?

目前,这就是我制作一个尺寸的网格所需的全部内容:

 grid = [[0,0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0,0]]

最佳答案

代码:

#defining size
x = y = 5
#create empty list to hold rows
grid = []
#for each row defined by Y, this loop will append a list containing X occurrences of "0"
for row in range(y):
    grid.append(list("0"*x))

print grid

输出:

[['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0']]

关于python - 如何用Python制作动态网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40062870/

相关文章:

model-view-controller - MVC 兼容网格,将具有 Excel 功能

python - 这个 Python 导入是如何工作的?

导入 matplotlib.pyplot 时 Python 崩溃

css - 导航栏标题没有向左移动

delphi - DevExpress QuantumGrid 的 "Hello, world!"示例?

html - 具有固定行和列标题的表格

python - 如何根据单词列表对字符串的单词进行分组?

python - Mac OS X 中的 virtualenv 问题

Python 日志记录级别似乎不起作用

javascript - ShieldUI 网格 : how to get the row where the doubleclick is fired