0
   1  
  3 2
 6 5 4
10 9 8 7
10 9 8 7 
 6 5 4 
  3 2 
   1

userInput=5

for i in range(1,userInput+1):
    for s in range (userInput - i) :    # s is equivalent to to spaces
        print(" ", end="")
    for j in range(1,(i * 2) ):
        print(j, end="")            
    print()

for i in range(userInput-1, 0, -1):
    for s in range (userInput - i) :
        print(" ", end="")
    for j in range(1,(i * 2)):
        print(j, end="")           
    print()    


    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

This is the output am getting

i am trying to get this format,but i am getting continuous number.Even the rows are in odd format .Any suggestions please

2 Answers2

0

Here's a solution using list comprehensions and some string manipulation:

def tri(num):
    return num * (num + 1) // 2

def make_list(size, depth=1):
    shape = " " * (size - depth - 1)
    shape += " ".join((str(tri(depth) - x) for x in range(depth)))
    if depth == size:
        return []
    return make_list(size, depth + 1) + [shape]

def print_dia(size):
    dia = make_list(size)
    print("\n".join(dia[::-1]))
    print("\n".join(dia))

if __name__ == "__main__":
    print_dia(5)

I generate each line by using the overall size and the current line number to produce the correct indentation, starting value (a triangular number), and the number of values on that line. I then produce a list of lines, print the reversed list first, then the original list, separated by newlines.

Dillon Davis
  • 6,679
  • 2
  • 15
  • 37
-1
userInput=5    
temp=0

for i in range(1,userInput+1):
    temp+=i
    for s in range(0,userInput-i):
        print(" ",end="")    
    for j in range(i):        
        print(temp-j,end=" ")         
    print()
for j in range(userInput,0,-1):
    for s in range(userInput-j):
        print(" ",end="")
    for j in range(j):
        print(temp,end=" ")
        temp-=1
    print()

@arthur_curry...i just used for loops for desired output..i have used a temp variable to keep the count and added with i(each stage value).Printing the values in descending.

Parzival
  • 29
  • 4