Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

pyramid of numbers in python

Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run:

            1

          2 1 2

        3 2 1 2 3

      4 3 2 1 2 3 4 

    5 4 3 2 1 2 3 4 5

  6 5 4 3 2 1 2 3 4 5 6 

7 6 5 4 3 2 1 2 3 4 5 6 7

I have the following:

num = eval(raw_input("Enter an integer from 1 to 15: "))

if num < 16:

      for i in range(1, num + 1):
          # Print leading space
          for j in range(num -  i,  0,  -1):
               print(" "),
          # Print numbers      
          for j in range(i, 0, -1):
               print(j),
          for j in range(2, i + 1):
               print(j),
           print("") 
else: 
 print("The number you have entered is greater than 15.")

This yields a misalignment for numbers greater than or equal to 10.

I have tried print(format(j, "4d")) and all the numbers become misaligned.

Any tips? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a leading space for a number ("01" - "09", "10", ...)

num = eval(raw_input("Enter an integer from 1 to 15: "))                                                                                           

def as_str(i):
    s = ""
    if i <10: s = " "
    return s + str(i)


#num = 15                                                                                                                                           

allrows = ""
for j in range(1,num+2):

    #leading spaces                                                                                                                                 
    row = " "*3*(num-j+1)

    #backward                                                                                                                                       
    for i in range(j-1,1,-1):
        s = as_str(i)
        row+=s + " "

    #forward                                                                                                                                        
    for i in range(1,j):
        s = as_str(i)
        row+=s + " "


    row +="
"
    allrows +=row

print allrows

Output

                                           1 
                                        2  1  2 
                                     3  2  1  2  3 
                                  4  3  2  1  2  3  4 
                               5  4  3  2  1  2  3  4  5 
                            6  5  4  3  2  1  2  3  4  5  6 
                         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 
                   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 
            11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 
         12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 
      13 12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 13 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...