以下是三種不同的圣誕樹的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)
輸出結果:
*********
*******
*****
***
*
|