Properly formatted multiplication table

前端 未结 5 1506
青春惊慌失措
青春惊慌失措 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:          


        
相关标签:
5条回答
  • 2021-02-15 11:29

    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:30

    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:37

    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:37

    Creating Arithmetic table is much simpler but i thought i should post my answer despite the fact there are so many answers to this question because no one talked about limit of table.

    Taking input from user as an integer

    num = int(raw_input("Enter your number"))
    

    Set limit of table, to which extent we wish to calculate table for desired number

    lim = int(raw_input("Enter limit of table"))
    

    Iterative Calculation starting from index 1

    In this, i've make use of slicing with format to adjust whitespace between number i.e., {:2} for two space adjust.

    for b in range(1, lim+1):
        print'{:2} * {:2} = {:2}'.format(a, b, a*b)
    

    Final CODE:

    num = int(raw_input("Enter your number"))
    lim = int(raw_input("Enter limit of table"))
    for b in range(1, lim+1):
        print'{:2} * {:2} = {:2}'.format(a, b, a*b)
    

    OUTPUT:

    Enter your number 2
    Enter limit of table 20
     2 *  1 =  2
     2 *  2 =  4
     2 *  3 =  6
     2 *  4 =  8
     2 *  5 = 10
     2 *  6 = 12
     2 *  7 = 14
     2 *  8 = 16
     2 *  9 = 18
     2 * 10 = 20
     2 * 11 = 22
     2 * 12 = 24
     2 * 13 = 26
     2 * 14 = 28
     2 * 15 = 30
     2 * 16 = 32
     2 * 17 = 34
     2 * 18 = 36
     2 * 19 = 38
     2 * 20 = 40
    
    0 讨论(0)
  • 2021-02-15 11:40

    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)
提交回复
热议问题