为了打印乘法表,我使用了一个 for 循环,该循环仅具有最多 10 次乘法表的固定值。我需要乘法表作为运行时值。是否有替代循环在运行时获取值?这是我尝试过的代码:
scanf(&num);
for(i=num;i<=num;i++)
for (j=1;j<=10;j++)
最佳答案
这不是 Python For 循环
在Python中,你可以使用Range函数来制作一个漂亮的乘法表。
def table_choice(my_choice=None):
for a in range(10):
if my_choice != None:
print('{0} x {1} = {2}'.format(my_choice, a, my_choice * a))
else:
for b in range(10):
print('{0} x {1} = {2}'.format(a, b, a * b))
table_choice(my_choice = 7)
输出:
7 x 0 = 0
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
如果您执行table_choice(),您将获得完整的表格
请参阅范围文档:https://docs.python.org/3/library/functions.html#func-range
关于c++ - 还有其他方法可以为乘法表编写循环以获取运行时值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58395221/