1,打印一个三角形,使用while,完成以下图形的输出** ** * ** * * ** * * * ** * * ** * ** **代码如下:h=1while h<=5: w=1 while w<=h: print("x",end="") w+=1 print("") h+=1h=5while h>=1: w=5 while w>=h: print("x",end="") w-=1 print("") h-=1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx可我打印的结果是这样,我实在想不明白怎么让第6行依次减少一个X请高手解答!谢谢!
3 回答

慕妹1056218
TA贡献1条经验 获得超0个赞
i=1
while i<=5:
j=1
while j<=i:
print('*',end='')
j+=1
print()
i+=1
i=1
while i<=4:
j=4
while j>=i:
print('*',end='')
j-=1
print()
i+=1

GCT1015
TA贡献1827条经验 获得超4个赞
def print_pic(num):
print ''.join(['*'] * num)
i = 1
h = 5
while i <= (2 * h - 1):
print_pic(i if i <= h else (2 * h - i))
i = i + 1
i = 1
h = 5
while i <= (2 * h - 1):
if i <= h:
print ''.join(['*'] * i)
else:
print ''.join(['*'] * (2 * h - i))
i = i + 1
# 递归
def print_pic(num, index=1):
count = index if index <= num else 2 * num - index
print ''.join(['*'] * count)
if index > 2 * num - 1:
return
print_pic(num, index=index+1)
print_pic(5)
- 3 回答
- 0 关注
- 3177 浏览
添加回答
举报
0/150
提交
取消