猿问

python3 while循环嵌套中的问题 打印倒三角型

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
请高手解答!谢谢!


摇曳的蔷薇
浏览 3178回答 3
3回答

慕妹1056218

i=1 while i<=5: j=1 while j<=i: print(&#039;*&#039;,end=&#039;&#039;) j+=1 print() i+=1 i=1 while i<=4: j=4 while j>=i: print(&#039;*&#039;,end=&#039;&#039;) j-=1 print() i+=1

GCT1015

def print_pic(num):&nbsp; print ''.join(['*'] * num)i = 1h = 5while i <= (2 * h - 1):&nbsp; print_pic(i if i <= h else (2 * h - i))&nbsp; i = i + 1i = 1h = 5while i <= (2 * h - 1):&nbsp; if i <= h:&nbsp; &nbsp; print ''.join(['*'] * i)&nbsp; else:&nbsp; &nbsp; print ''.join(['*'] * (2 * h - i))&nbsp; i = i + 1# 递归def print_pic(num, index=1):&nbsp; count = index if index <= num else 2 * num - index&nbsp; print ''.join(['*'] * count)&nbsp; if index > 2 * num - 1:&nbsp; &nbsp; return&nbsp; print_pic(num, index=index+1)print_pic(5)
随时随地看视频慕课网APP
我要回答