减少角色python之间的空格

Jaf*_*son -1 python python-2.7

我正在尝试打印楼梯图案.我编写的代码如下:

def StairCase(n):
    for i in range(1, n + 1):
        stair_array = []
        j = 1
        while (j <= n):
            if (j <= i):
                stair_array.append('#')
            else:
                stair_array.append(' ')
            j = j + 1

        reversed_array = list(reversed(stair_array))
        for element in reversed_array:
            print "{}".format(element),
        print


_n = int(raw_input().strip("\n"))
StairCase(_n)
Run Code Online (Sandbox Code Playgroud)

我得到的输出为:

6
          #
        # #
      # # #
    # # # #
  # # # # #
# # # # # #
Run Code Online (Sandbox Code Playgroud)

预期的产出是:

6
     #
    ##
   ###
  ####
 #####
######
Run Code Online (Sandbox Code Playgroud)

正如我可以看到我的输出是空格的,并且根据原始模式是不可接受的.请帮助.

Eci*_*ana 5

如果你坚持:

def StairCase(n):
    for i in range(1, n + 1):
        stair_array = []
        j = 1
        while (j <= n):
            if (j <= i):
                stair_array.append('#')
            else:
                stair_array.append(' ')
            j = j + 1

        reversed_array = list(reversed(stair_array))
        print ''.join(reversed_array)
Run Code Online (Sandbox Code Playgroud)

但更简单的方法是:

def StairCase_new(n):
    for i in range(1, n + 1):
        print ' '*(n-i) + '#'*i
Run Code Online (Sandbox Code Playgroud)