第一种:
*
**
***
****
*****
代码:
row = 1
while row <= 5:
col = 1
while col <= row:
print('*',end='')
col += 1
print('')
row += 1

运行:

第二种:
*****
****
***
**
*
代码:
row = 5
while row >= 1:
col = 1
while col <= row:
print('*',end='')
col += 1
print('')
row -=1

运行:

第三种:
*
**
***
****
*****
代码:
for i in range(6):
print(' '*(6-i),end='')
print('*'* i)

运行:

或者:
row = 0
while row <= 5:
col = 0
while col < 5-row:
col += 1
print(' ',end= '')
while col < 5:
print('*',end='')
col += 1
print()
row += 1

运行:

第四种:
*****
****
***
**
*
代码:
for i in range(5):
print(' '*i,end='*')
print('*'*(4-i))

运行:

或者:
row = 0
while row <= 5:
col = 0
while col < row:
col += 1
print(' ',end= '')
while col >= row and col < 5:
print('*',end='')
col += 1
print()
row += 1

运行:

本文详细介绍了使用Python编程语言实现五种不同星号(*)图案的打印方法,包括正三角形、倒三角形、左对齐三角形、右对齐三角形和金字塔形。通过循环结构和字符串操作,展示了如何控制星号的排列和空格的插入,以形成各种几何形状。

2868

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



