如何使用嵌套for循环打印乘法表

时间:2016-06-09 04:57:35

标签: python for-loop printing

我需要一些使用嵌套for循环打印乘法表的帮助。我现在的代码是:

for x in range(1,10):
   print("  ", x, end = '')
   print()
for row in range(1, 10):
    for col in range(1, 10):
        num = row * col
        if num < 10:
            empty = "  "
        else:
            if num < 100: 
                empty  = " " 
        print(empty, num, end = '')
    print()

它出现了

And it comes out as

我需要它打印为

I need it to print as

3 个答案:

答案 0 :(得分:2)

最简单的方法之一是从0而不是1开始计数:

for row in range(0, 10):
    for col in range(0, 10):
        num = row * col
        if num < 10:
            empty = "  "
        else:
            if num < 100: 
                empty  = " " 
        if col == 0:
            if row == 0:
                print("    ", end = '')
            else:
                print("  ", row, end='')
        elif row == 0:
            print("  ", col, end='')
        else:
            print(empty, num, end = '')
    print()

这将打印

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

话虽如此,这是一个单线版本,产生相同的输出:

print("\n".join(("{:>4}"*10).format(*(i*j if i*j else i+j if i+j else "" for j in range(10))) for i in range(10)))

答案 1 :(得分:2)

试试这个:

header = '\t{}'.format('\t'.join(map(str, range(1, 10))))
rows = []
for i in range(1, 10):
   row = '\t'.join(map(str, (i*q for q in range(1, 10))))
   rows.append('{}\t{}'.format(i, row))
print(header+'\n' + '\n'.join(rows))

哪个会给你:

>>> print(header+'\n'+'\n'.join(rows))
        1       2       3       4       5       6       7       8       9
1       1       2       3       4       5       6       7       8       9
2       2       4       6       8       10      12      14      16      18
3       3       6       9       12      15      18      21      24      27
4       4       8       12      16      20      24      28      32      36
5       5       10      15      20      25      30      35      40      45
6       6       12      18      24      30      36      42      48      54
7       7       14      21      28      35      42      49      56      63
8       8       16      24      32      40      48      56      64      72
9       9       18      27      36      45      54      63      72      81

@Selcuk做了一些很好的观察,所以这里有一个嵌套for循环的版本,以及正确的对齐方式:

header = []
for i in range(1, 10):
    header.append('{0:>2}'.format(i))

rows = []
for i in range(1, 10):
   row = []
   for q in range(1, 10):
     result = '{0:>2}\t'.format(i*q)
     row.append(result)
   rows.append(row)

print('\t{0:>2}'.format('\t'.join(header)))
for idx, row in enumerate(rows):
   print('{0:>2}\t{1}'.format(idx+1, ''.join(row)))

答案 2 :(得分:0)

这是用于打印自定义长度表格的功能

def multiplication_table(row, col):
    fin = []
    for i in range(1, row+1):
        arr = []
        for j in range(1, col+1):
            arr.append(i * j)
        fin.append(arr)
    return fin

例如:multiplication_table(3,3)

[[1, 2, 3], 
 [2, 4, 6], 
 [3, 6, 9]]

希望有帮助!