1,打印一个三角形, 使用while,完成以下图形的输出
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
代码如下:
h=1
while h<=5:
w=1
while w<=h:
print("x",end="")
w+=1
print("")
h+=1
h=5
while h>=1:
w=5
while w>=h:
print("x",end="")
w-=1
print("")
h-=1
x
xx
xxx
xxxx
xxxxx
x
xx
xxx
xxxx
xxxxx
可我打印的结果是这样,我实在想不明白怎么让第6行依次减少一个X请高手解答!谢谢!
3 回答
![?](http://img1.sycdn.imooc.com/54584c5e0001491102200220-100-100.jpg)
慕娘9325324
TA贡献1783条经验 获得超4个赞
我也是萌新 加油共勉
h=5
while h>=1:
w=1
while h>w:
print("x",end="")
w+=1
print("")
h-=1
![?](http://img1.sycdn.imooc.com/54584eff000195a302200220-100-100.jpg)
慕虎7371278
TA贡献1802条经验 获得超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)
![?](http://img1.sycdn.imooc.com/5458655200013d9802200220-100-100.jpg)
慕慕森
TA贡献1856条经验 获得超17个赞
i = 1
while i <= 5:
j = 1
while j <= i:
print("x",end=" ")
j += 1
print("")
i += 1
if i==6:
i = 5
while i>=1:
j = 1
while i>j:
print("x",end=" ")
j += 1
print("")
i -= 1
break
添加回答
举报
0/150
提交
取消