Creating a diamond pattern using loops

后端 未结 5 399
予麋鹿
予麋鹿 2021-01-03 17:17

I am trying to write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For Example, if the side length is 4, the prog

5条回答
  •  隐瞒了意图╮
    2021-01-03 17:32

    How about this:

    n = 5
    stars = [('*' + '**'*i).center(2*n + 1) for i in range(n)]
    
    print('\n'.join(stars + stars[::-1][1:]))
    

    The output:

         *     
        ***    
       *****   
      *******  
     ********* 
      *******  
       *****   
        ***    
         *    
    

    This is similar to some of the answers above, but I find the syntax to be more simple and readable.

提交回复
热议问题