Python Spaced Triangle

前端 未结 4 1700
感情败类
感情败类 2021-01-22 00:17

I\'m supposed to write a program that ends up such as this:

*     *
 *   *
  * *
   *

I have the code written for a regular one, but I\'m not s

4条回答
  •  温柔的废话
    2021-01-22 00:50

    Try:

    def triangle(i, t = 0):
        if i == 0:
            print (t+1) *' '+ '*'
    
        else:
            print ' ' * (t + 1)+ '*' + ' ' * (i * 2 - 1) + '*'
            triangle(i - 1, t + 1)
    
    triangle(5)
    

    this code print:

     *         *
      *       *
       *     *
        *   *
         * *
          *
    

提交回复
热议问题