目录
1.打印小星星

方法一:while循环
i=1
while i<=5:
print(i*'*')
i+=1
方法二:while嵌套循环
while i <= 5:
j = 1
while j <= i:
print('*', end='')
j += 1
print()
i += 1
2.打印一颗圣诞树

i = 1
while i <= 5:
print((5 - i) * ' ', (2 * i - 1) * '*')
i += 1
i = 1
while i <= 5:
print((5 - i) * ' ', (2 * i - 1) * '*')
i += 1
i = 1
while i <= 3:
print(4 * ' ', 2 * '*', 17 * ' ')
i += 1
3.打印两棵圣诞树

i = 1
while i <= 5:
print((5 - i) * ' ', (2 * i - 1) * '*', (20 - 2 * i + 1) * ' ', (2 * i - 1) * '*')
i += 1
i = 1
while i <= 5:
print((5 - i) * ' ', (2 * i - 1) * '*', (20 - 2 * i + 1) * ' ', (2 * i - 1) * '*')
i += 1
i = 1
while i <= 3:
print(4 * ' ', 2 * '*', 17 * ' ', 2 * '*')
i += 1

这篇博客介绍了如何使用Python通过循环来打印不同形式的小星星,进而构建圣诞树的图案,包括单棵和双棵圣诞树的实现方法。通过两种while循环的方式详细阐述了绘制过程。
683

被折叠的 条评论
为什么被折叠?



