使用Python中嵌套循环的双位数字乘法表

时间:2014-04-16 12:00:38

标签: python python-2.7 for-loop nested-loops

我正在尝试学习嵌套循环,所以我想制作一个简单的乘法表,请你帮我改进代码?

for i in range(1,10):
    print("i =", i, ":")
    for j in range(1, 10):
        print (i*j)

当我运行它时,我的程序看起来非常混乱,如何将结果与矩阵/表格对齐以使其更易于阅读?

3 个答案:

答案 0 :(得分:3)

如果您使用的是Python 2.x,print是一个声明,因此您需要确保在打印当前行后该行不会结束。 {:2d}函数中的format确保您始终拥有两位数的空格。如果您更喜欢0而不是空格,则可以使用{:02d}

for i in range(1, 10):
    print "i =", i, ":",                   # Note the comma at the end
    for j in range(1, 10):
        print "{:2d}".format(i * j),       # Note the comma at the end
    print

<强>输出

i = 1 :  1  2  3  4  5  6  7  8  9
i = 2 :  2  4  6  8 10 12 14 16 18
i = 3 :  3  6  9 12 15 18 21 24 27
i = 4 :  4  8 12 16 20 24 28 32 36
i = 5 :  5 10 15 20 25 30 35 40 45
i = 6 :  6 12 18 24 30 36 42 48 54
i = 7 :  7 14 21 28 35 42 49 56 63
i = 8 :  8 16 24 32 40 48 56 64 72
i = 9 :  9 18 27 36 45 54 63 72 81

print中的小变化也可以使它在Python 3.x中运行。请记住,print是Python 3.x中的一个函数

for i in range(1, 10):
    print("i =", i, ":", end=" ")
    for j in range(1, 10):
        print("{:2d}".format(i * j), end=" ")
    print()

注意:您可以在Python文档的Format Specification Mini-Language部分中详细了解各种字符串格式选项。

答案 1 :(得分:0)

你想要这样的东西吗?:

for i in range(1, 10):
    print '\n','{:02}'.format(i),':',
    for j in range(1, 10):
        print '{:02}'.format(i*j),

[OUTPUT]
01 : 01 02 03 04 05 06 07 08 09 
02 : 02 04 06 08 10 12 14 16 18 
03 : 03 06 09 12 15 18 21 24 27 
04 : 04 08 12 16 20 24 28 32 36 
05 : 05 10 15 20 25 30 35 40 45 
06 : 06 12 18 24 30 36 42 48 54 
07 : 07 14 21 28 35 42 49 56 63 
08 : 08 16 24 32 40 48 56 64 72 
09 : 09 18 27 36 45 54 63 72 81

OR:

for i in range(1, 10):
    for j in range(1, 10):
        print '{:02}'.format(i),'X','{:02}'.format(j),'=','{:02}'.format(i*j)
    print

[OUTPUT]
01 X 01 = 01
01 X 02 = 02
01 X 03 = 03
01 X 04 = 04
01 X 05 = 05
01 X 06 = 06
01 X 07 = 07
01 X 08 = 08
01 X 09 = 09

02 X 01 = 02
02 X 02 = 04
02 X 03 = 06
02 X 04 = 08
02 X 05 = 10
02 X 06 = 12
02 X 07 = 14
02 X 08 = 16
02 X 09 = 18

03 X 01 = 03
03 X 02 = 06
03 X 03 = 09
03 X 04 = 12
03 X 05 = 15
03 X 06 = 18
03 X 07 = 21
03 X 08 = 24
03 X 09 = 27

...
...
...
...

答案 2 :(得分:0)

尝试一下

for a in range(1,6):
    print(a,'tables')
    for b in range(1,11):
        c=a*b
        print(a,'X',b,'=', c)
    print()

Just replace 6 by a number which you want and 1 to that (eg: if you 
want up to 10 replace 6 with 11)  

输出

1 tables
1 X 1 = 1
1 X 2 = 2
1 X 3 = 3
1 X 4 = 4
1 X 5 = 5
1 X 6 = 6
1 X 7 = 7
1 X 8 = 8
1 X 9 = 9
1 X 10 = 10

2 tables
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20

3 tables
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30

4 tables
4 X 1 = 4
4 X 2 = 8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36
4 X 10 = 40

5 tables
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50