简体   繁体   中英

How to print multiplication table using nested for loops

I need some help printing multiplication table using nested for loop. My code right now is:

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

我需要它打印为

One of the simplest methods would be to start counting from 0 instead of 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()

this will 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

That being said, here is a one-liner version that results in the same output:

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)))

Try this:

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))

Which will give you:

>>> 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 made some good observations, so here is a version with nested for loops, and correct alignment as well:

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)))

Here is a function for printing a table of custom length

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

Ex: multiplication_table(3, 3)

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

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM