还有其他方法可以为乘法表编写循环以获取运行时值吗?

Any other way to write a loop for multiplication table to take run time values?

本文关键字:获取 循环 运行时 方法 其他      更新时间:2023-10-16

要打印乘法表,我使用的是 for 循环,它最多只有第 10 次表具有固定值。我需要乘法表作为运行时值。是否有替代循环在运行时获取值?这是我尝试过的代码:

scanf(&num); 
for(i=num;i<=num;i++) 
for (j=1;j<=10;j++) 

这不是一个Python For loop

。在python中,你可以使用范围函数来拥有一个漂亮的乘法表。

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 中的范围文档