Python Spaced Triangle

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

    you should be using a for loop for this, recursion works but it is not the best idea to use it all the time. this is what i did:

    def GioTri(i):
    
    foo = i - 1
    bar = 0
    
    for i in range(i-1):
    
    
        print ' ' * bar + "*" + " " * (foo*2 - 1) + "*" + " " * bar
        foo = foo - 1
        bar = bar + 1
    
    
    print " " * bar + "*" + " " * bar 
    

    the result of this looks like this:

    *     *
     *   * 
      * *  
       *   
    

提交回复
热议问题