我想通过提示用户输入1到16之间的值并使用这些值填充矩阵来创建4倍4矩阵/表。我用过:
ROWS = 4
COLUMNS = 4
square_matrix = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
number = int(input("Enter a value between 1 and 16: "))
for i in range(ROWS):
for j in range(COLUMNS):
square_matrix[i][j] = number
但这会以相同的价值填充所有内容。我希望程序在每次提示后逐渐填充矩阵,直到输入第16个数字。我怎么能这样做?
答案 0 :(得分:1)
您可以在for循环中移动输入。像这样:
ROWS = 4
COLUMNS = 4
square_matrix = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for i in range(ROWS):
for j in range(COLUMNS):
number = int(input("Enter a value between 1 and 16: "))
square_matrix[i][j] = number