Python Spaced Triangle

前端 未结 4 1706
感情败类
感情败类 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:39

    Building on @kharazi's answer (because this reminds me of my early GWBasic programming which is what got me excited about programming as a kid):

    def triangle(i, leftShape='*', rightShape='*', bottomShape='*', spaceShape=' ', t = 0):
        if i <= 0:
            print ((t+1)*spaceShape)+bottomShape+((t+1)*spaceShape)
        else:
            print (spaceShape*(t + 1))+leftShape+(spaceShape*(i*2-1))+rightShape+(spaceShape*(t + 1))
            triangle(i-1, leftShape, rightShape, bottomShape, spaceShape, t+1)
    
    if __name__== '__main__':
        triangle(3)
        triangle(3, '\\', '/')
        triangle(3, '\\', '/', '~')
        triangle(5, '╚╗', '╔╝', '╚╦╝')
        triangle(5, '╚╗', '╔╝', '╚╦╝', '|')
        triangle(-2)
    

    Produces the following output:

    triangle(3)
     *     *
      *   *
       * *
        *
    
    triangle(3, '\\', '/')
     \     /
      \   /
       \ /
        *
    
    triangle(3, '\\', '/', '~')
     \     /
      \   /
       \ /
        ~
    
    triangle(5, '╚╗', '╔╝', '╚╦╝')
     ╚╗         ╔╝
      ╚╗       ╔╝
       ╚╗     ╔╝
        ╚╗   ╔╝
         ╚╗ ╔╝
          ╚╦╝
    
    triangle(5, '╚╗', '╔╝', '╚╦╝', '|')
    |╚╗|||||||||╔╝|
    ||╚╗|||||||╔╝||
    |||╚╗|||||╔╝|||
    ||||╚╗|||╔╝||||
    |||||╚╗|╔╝|||||
    ||||||╚╦╝||||||
    
    triangle(-2)
     *
    

提交回复
热议问题