以下是三种不同的圣诞树的Python代码:
def draw_solid_tree(height):
for i in range(height):
print(' ' * (height - i - 1) + '*' * (2*i + 1))
print(' ' * (height - 1) + '|')
# 调用函数绘制实心圣诞树
draw_solid_tree(5)
输出结果:
*
***
*****
*******
*********
|
def draw_hollow_tree(height):
for i in range(height):
if i == height - 1:
print(' ' * (height - i - 1) + '*' * (2*i + 1))
else:
print(' ' * (height - i - 1) + '*' + ' ' * (2*i - 1) + '*')
print(' ' * (height - 1) + '|')
# 调用函数绘制空心圣诞树
draw_hollow_tree(5)
输出结果:
*
* *
* *
* *
*********
|
def draw_inverse_tree(height):
for i in range(height, 0, -1):
print(' ' * (height - i) + '*' * (2*i - 1))
print(' ' * height + '|')
# 调用函数绘制倒立圣诞树
draw_inverse_tree(5)
输出结果:
*********
*******
*****
***
*
|