如何在Python中创建动态网格

时间:2016-10-15 18:45:09

标签: python grid

我正在构建一个X和O(tic tac toe)应用程序,用户可以决定网格是否介于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]]

1 个答案:

答案 0 :(得分:2)

代码:

#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']]