samedi 7 novembre 2020

Formatting number "hourglass"/ shapes in python

I am new to python and are trying to make "fun" number shapes. I am struggling understand how to use the formatting tricks. I am trying to make an hour glass that starts at n (aka n-1) goes down to 1 and then counts back up to n. Each line should have the same value on it, with each line adding +2 additional values. I have managed to get the bottom 1/2 of the hour glass. However, I am really struggling with the top piece of the hourglass. I've just been shuffling crap around and have managed to get here:

    [IN]: 
    n = 6 
for i in range(n-1, 1, -1):     #TOP
    for j in range(1, 2*i):
        print(i, end='')
    print( )          
for i in range(1,n):             #BOTTOM
    for j in range((n-1),i,-1):
        print(" ",end='')
    for k in range(1,2*i):
        print(i,end='')
    print()
    
[OUT]:
555555555
4444444
33333
222
    1
   222
  33333
 4444444
555555555

As I said the bottom 1/2 is formatted perfectly but I just can't figure out what to do to the top. All help would be greatly appreciated!

Thank you

Aucun commentaire:

Enregistrer un commentaire