Properly formatted multiplication table

前端 未结 7 2220
时光取名叫无心
时光取名叫无心 2021-02-15 11:11

How would I make a multiplication table that\'s organized into a neat table? My current code is:

n=int(input(\'Please enter a positive integer between 1 and 15:          


        
相关标签:
7条回答
  • 2021-02-15 11:20

    Quick way (Probably too much horizontal space though):

    n=int(input('Please enter a positive integer between 1 and 15: '))
    for row in range(1,n+1):
        for col in range(1,n+1):
            print(row*col, end="\t")
        print()
    

    Better way:

    n=int(input('Please enter a positive integer between 1 and 15: '))
    for row in range(1,n+1):
        print(*("{:3}".format(row*col) for col in range(1, n+1)))
    

    And using f-strings (Python3.6+)

    for row in range(1, n + 1):
        print(*(f"{row*col:3}" for col in range(1, n + 1)))
    
    0 讨论(0)
  • 2021-02-15 11:20

    this one looks pretty neat:

       print '\t\t\t======================================='
       print("\t\t\t\tMultiplication Tables")
       print '\t\t\t=======================================\n'
       for i in range(1,11):
           print '\t', i,
       print
       print("___________________________________________________________________________________________________________________")
    
       for j in range(1,11):
           print("\n")
           print j, '|',
           for k in range(1,11):
               print '\t', j * k,
       print("\n")
    
    0 讨论(0)
  • 2021-02-15 11:22

    For this print as following

     print "%d X %d"%(row, col)
    

    It will print as 2 X 3.

    0 讨论(0)
  • 2021-02-15 11:23

    Gnibbler's approach is quite elegant. I went for the approach of constructing a list of list of integers first, using the range function and taking advantage of the step argument.

    for n = 12

    import pprint
    n = 12
    m = list(list(range(1*i,(n+1)*i, i)) for i in range(1,n+1))
    pprint.pprint(m)
    [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
     [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
     [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
     [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
     [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
     [6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
     [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
     [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
     [9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
     [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
     [11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
     [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]]
    

    Now that we have a list of list of integers that is in the form that we want, we should convert them into strings that are right justified with a width of one larger than the largest integer in the list of lists (the last integer), using the default argument of ' ' for the fillchar.

    max_width = len(str(m[-1][-1])) + 1
    for i in m:
        i = [str(j).rjust(max_width) for j in i]
        print(''.join(i))
    
       1   2   3   4   5   6   7   8   9  10  11  12
       2   4   6   8  10  12  14  16  18  20  22  24
       3   6   9  12  15  18  21  24  27  30  33  36
       4   8  12  16  20  24  28  32  36  40  44  48
       5  10  15  20  25  30  35  40  45  50  55  60
       6  12  18  24  30  36  42  48  54  60  66  72
       7  14  21  28  35  42  49  56  63  70  77  84
       8  16  24  32  40  48  56  64  72  80  88  96
       9  18  27  36  45  54  63  72  81  90  99 108
      10  20  30  40  50  60  70  80  90 100 110 120
      11  22  33  44  55  66  77  88  99 110 121 132
      12  24  36  48  60  72  84  96 108 120 132 144
    

    and demonstrate the elasticity of the spacing with a different size, e.g. n = 9

    n=9
    m = list(list(range(1*i,(n+1)*i, i)) for i in range(1,n+1))
    for i in m:
        i = [str(j).rjust(len(str(m[-1][-1]))+1) for j in i]
        print(''.join(i))
    
      1  2  3  4  5  6  7  8  9
      2  4  6  8 10 12 14 16 18
      3  6  9 12 15 18 21 24 27
      4  8 12 16 20 24 28 32 36
      5 10 15 20 25 30 35 40 45
      6 12 18 24 30 36 42 48 54
      7 14 21 28 35 42 49 56 63
      8 16 24 32 40 48 56 64 72
      9 18 27 36 45 54 63 72 81
    
    0 讨论(0)
  • 2021-02-15 11:27

    Or you could just do this (not as simplistic as the others but it works):

    def main():
    
        rows = int(input("Enter the number of rows that you would like to create a multiplication table for: "))
        counter = 0
        multiplicationTable(rows,counter)
    
    def multiplicationTable(rows,counter):
    
        size = rows + 1
    
        for i in range (1,size):
            for nums in range (1,size):
                value = i*nums
                print(value,sep=' ',end="\t")
                counter += 1
                if counter%rows == 0:
                    print()
                else:
                    counter
    main()
    
    0 讨论(0)
  • 2021-02-15 11:29
    for i in range(1, 10) :
        for j in range(1, 10):
            print(repr(i*j).rjust(4),end=" ")
    print()
    print()
    

    Output:

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

    or this one

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

    the result is :

     1     2     3     4     5     6     7     8     9    10
     2     4     6     8    10    12    14    16    18    20
     3     6     9    12    15    18    21    24    27    30
     4     8    12    16    20    24    28    32    36    40
     5    10    15    20    25    30    35    40    45    50
     6    12    18    24    30    36    42    48    54    60
     7    14    21    28    35    42    49    56    63    70
     8    16    24    32    40    48    56    64    72    80
     9    18    27    36    45    54    63    72    81    90
    10    20    30    40    50    60    70    80    90   100
    
    0 讨论(0)
提交回复
热议问题