我不需要答案,如果可能的话,只需要一个起点。
我已经做了一个可以打印1-10的乘法表,但我的教授希望看到这个表存储在2D列表中。我有我当前的代码(工作代码,在这里张贴),以及我专门为"2D列表“版本制作的新代码,但我在这两个版本上都一无所获。
EDIT:我可能应该澄清一下,代码正在做我想要它做的事情,那就是输出一个乘法表,但我在将它转换为2D列表时遇到了麻烦。
#Initial list and global variables
MT = ["X", 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]
mult = 1
jump = 0
repeat = 1
list = 1
while repeat < 12:
index = 1
mult = 1
jump += 1
MT.append(MT[list])
while (index < 11):
MT.append(mult * jump)
mult += 1
index += 1
else:
repeat += 1
list += 1
else:
print("Multiplication Table:")
index = 0
while (index < 121):
if (((index + 1) % 11) != 0):
print(MT[index], end = "\t")
else:
print(MT[index], end = "\n")
index += 1
发布于 2018-07-22 05:17:42
我的第一个建议是从2D列表开始:
MT = [["X", 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]]
而不是你所拥有的。
在内部while循环之前开始一个新的空列表。可以使用第一个值new_row = [repeat]
对其进行初始化。在循环内部附加到new_row
而不是MT
在while循环完成之后,您可以将new_row
附加到MT
MT.append(new_row)
这应该会让你开始使用2d列表。但是,既然我有你在这里...
如果您需要重复并跟踪重复的次数,那么使用for循环而不是while循环。
例如,如果你想要一个从2到12的循环,你可以使用:
for repeat in range(2, 13):
print(repeat)
此外,永远不要覆盖内置关键字,因为这可能会导致意外的行为。所以给它起个名字叫list
,但我不认为这个变量是真的需要,因为它和repeat
很相似。
https://stackoverflow.com/questions/51460055
复制相似问题