如何在不同的乘法表之间插入换行符?

时间:2017-09-28 12:31:37

标签: python python-3.x for-loop newline multiplication

我编写了这段代码来打印出乘法表从1到9,但它打印出来时不同表之间没有换行。有谁知道如何解决这个问题?

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

结果如下:

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
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 个答案:

答案 0 :(得分:2)

for i in range(1, 10):循环的迭代之间打印一个空行,这会将表格与您打印乘法

的数字分开
for i in range(1, 4):
    for j in range(1, 4):
        k = i * j
        print(i,"x",j, "=", k)
    print()

>> 1 x 1 = 1
>> 1 x 2 = 2
>> 1 x 3 = 3

>> 2 x 1 = 2
>> 2 x 2 = 4
>> 2 x 3 = 6

>> 3 x 1 = 3
>> 3 x 2 = 6
>> 3 x 3 = 9

答案 1 :(得分:0)

for i in range(1, 10):
    for j in range(1, 10):
        k = i * j
        print(i,"x",j, "=", k, end='\n')