黑白版
# 圣诞树层数
LAYER = 4
# 圣诞树宽度
width = LAYER * 4 + 6
# 圣诞树上身
for i in range(1, LAYER + 1):
for j in range(3):
print(('*' * ((j + i) * 4 - 2)).center(width))
# 圣诞树下身
for _ in range(LAYER):
print('||'.center(width))
彩色版
def make_christmas_tree(layer):
width = layer * 4 + 6
for i in range(1, layer + 1):
for j in range(3):
print('\033[92m%s\033[0m' % ('*' * ((j + i) * 4 - 2)).center(width))
for _ in range(layer):
print('\033[33m%s\033[0m' % '||'.center(width))
if __name__ == '__main__':
make_christmas_tree(3)