我正在构建一个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]]
答案 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']]