输入一个整数,代表树干的高度,打印一棵‘圣诞树’

"""
 输入一个整数,代表树干的高度,打印一棵‘圣诞树’
 如:
 输入:3
 打印 :
       *
      ***
     *****
       *
       *
       *
"""
# 方法一
bole = int(input('请输入数干的高度:'))
count = 1 # 树叶部分计数器 树顶始终是一个 * ,记数顶层数
count_bole = 1 # 树干部分计数器
treetop_max = bole * 2 - 1 # 树顶从上往下数最下的一层
#打印树叶部分
while count <= bole:
    line = '*' * (2 * count - 1)
    print(line.center(treetop_max))
    count +=1
#打印树干部分
while count_bole <= bole:
    line_bole = '*'
    print(line_bole.center(treetop_max))
    count_bole += 1

#方法二
bole = int(input('请输入数干的高度:'))
#打印树叶部分
for ax in range(1,bole+1):
    space_num = bole -ax # 空格的个数
    print(' '*space_num + '*'*(2*ax-1)
    )
#打印树干部分
for bx in range(1,bole+1):
    print(' ' * (bole-1)+'*')

猜你喜欢

转载自www.cnblogs.com/touch-prc/p/12911851.html