【发布时间】:2018-04-25 22:27:48
【问题描述】:
我正在尝试打印楼梯图案。我写的代码如下:
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)
我得到的输出为:
6
#
# #
# # #
# # # #
# # # # #
# # # # # #
预期的输出是:
6
#
##
###
####
#####
######
正如人们所看到的,我的输出带有空格,并且按照原始模式是不可接受的。请帮忙。
【问题讨论】:
-
@pissall 那些是用来做楼梯的,不是为了“#”之间的空间
-
顺便说一句,您应该认真考虑学习 Python 3。Python 2 将在 2020 年正式结束生命周期。
-
链接的问题显示了使用
print语句和print函数执行此操作的各种方法。但是,最好使用''.join(element)执行此操作。或者,您可以使用字符串乘法而不是循环来生成字符串。 -
@PM2Ring 还有 3 年... :D :P
标签: python python-2.7