pythonfor-looppascals-triangle

Can someone write the code for n = 19 rows and get this following figure? For Python


Write a function that accepts a number n as an input, and it returns n rows that look like the following pattern. Run your function for n = 19 (the output below is for n=19).

n = int(input("enter number of rows:"))
for i in range(1, n+1):
    for j in range(1, n-i+1):
       print(end=' ')
    for j in range(i,0, -1):
        print(''+str(j),end='')
    for j in range(2,i+1):
        print(str(j)+'_',end='')
    print()

Ouput

                  1
                 212_
                3212_3_
               43212_3_4_
              543212_3_4_5_
             6543212_3_4_5_6_
            76543212_3_4_5_6_7_
           876543212_3_4_5_6_7_8_
          9876543212_3_4_5_6_7_8_9_
         109876543212_3_4_5_6_7_8_9_10_
        11109876543212_3_4_5_6_7_8_9_10_11_
       1211109876543212_3_4_5_6_7_8_9_10_11_12_
      131211109876543212_3_4_5_6_7_8_9_10_11_12_13_
     14131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_
    1514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_
   161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_
  17161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_
 1817161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_
191817161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_

enter image description here


Solution

  • This will work and will give you a good answer on your assignment.

    n = int(input("enter number of rows:")) 
    r = 0
    for i in range(1, int((n+1)/2+1)):
        r += 1
        for j in range(1, n-i+1):
            print(end='  ') 
        for kk in range(i,0, -1):
            if i==1:
                print(' '+str(i))
            else:
                if kk==i:
                    print(end= ' ')
                if kk==1:
                    print(str(kk), end='')
                else:
                    print(str(kk)+'_', end='')
        for ll in range(2,i+1):
            if i>1:
                print('_'+str(ll), end='')
        if i>1:
            print()
        r += 1
        if r > n:
            break
        for jj in range(1, n-i+1):
            print(end='  ')
        for k in range(i,0, -1):
            if i==1:
                print('_'+str(i)+'_', end='')
            else:
                print('_'+str(k), end='')
        for l in range(2,i+1):
            if l==2:
                print('_'+str(l)+'_',end='') 
            else:
                print(str(l)+'_',end='') 
        print()
    

    Result:

                                     1
                                    _1_
                                   2_1_2
                                  _2_1_2_
                                 3_2_1_2_3
                                _3_2_1_2_3_
                               4_3_2_1_2_3_4
                              _4_3_2_1_2_3_4_
                             5_4_3_2_1_2_3_4_5
                            _5_4_3_2_1_2_3_4_5_
                           6_5_4_3_2_1_2_3_4_5_6
                          _6_5_4_3_2_1_2_3_4_5_6_
                         7_6_5_4_3_2_1_2_3_4_5_6_7
                        _7_6_5_4_3_2_1_2_3_4_5_6_7_
                       8_7_6_5_4_3_2_1_2_3_4_5_6_7_8
                      _8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_
                     9_8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_9
                    _9_8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_9_
                   10_9_8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_9_10