Creating a diamond pattern using loops

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

    This might work better for you:

    n = userInput
    
    for idx in range(n-1):
        print((n-idx) * ' ' + (2*idx+1) * '*')
    for idx in range(n-1, -1, -1):
        print((n-idx) * ' ' + (2*idx+1) * '*')
    

    Output for userInput = 6:

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

提交回复
热议问题