Creating a diamond pattern using loops

后端 未结 5 429
予麋鹿
予麋鹿 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:38

        def build(width):
            if width%2==0: 
                x=[(' *'*i).center(width*2,' ') for i in range(1,(width*2/2))]
            else: 
               x=[(' *'*i).center(width*2+1,' ') for i in range(1,((width*2+1)/2))]
            for i in x[:-1]+x[::-1]: print i
    

    This worked for me for any positive width, But there are spaces padding the *s

提交回复
热议问题