Properly formatted multiplication table

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

提交回复
热议问题