原文: https://www.programiz.com/python-programming/examples/multiplication-table
该程序显示变量num的乘法表(从 1 到 10)。
要理解此示例,您应该了解以下 Python 编程主题:
在下面的程序中,我们使用了for循环来显示 12 的乘法表。
源代码
# Multiplication table (from 1 to 10) in Pythonnum = 12# To take input from the user# num = int(input("Display multiplication table of? "))# Iterate 10 times from i = 1 to 10for i in range(1, 11):print(num, 'x', i, '=', num*i)
输出
12 x 1 = 1212 x 2 = 2412 x 3 = 3612 x 4 = 4812 x 5 = 6012 x 6 = 7212 x 7 = 8412 x 8 = 9612 x 9 = 10812 x 10 = 120
在这里,我们将for循环与range()函数“)一起使用进行了 10 次迭代。range()函数内部的参数为(1,11)。 表示大于或等于 1 且小于 11。
我们已经显示了变量num的乘法表(本例中为 12)。 您可以在上述程序中更改num的值以测试其他值。
