python之中用while循环打出四种星星形状

  • 第一种:
*
**
***
****
*****
i = 1
while i <= 5:
    j = 1
    while j <= i:
        print("*",end='')     # 不换行输出
        j += 1
    print()   #手动换行
    i += 1
  • 第二种
    *
   **
  ***
 ****
*****
i =1
while i <= 5:
    j = 1
    while j <= (5 - i):
        print(" ",end='')
        j += 1
    while (j >(5-i) and j <= 5):
        print("*",end='')
        j += 1
    print()
    i += 1
  • 第三种:
*****
****
***
**
*
i = 5
while i >= 0:
    j = 1
    while j <= i:
        print("*",end='')
        j += 1
    print()
    i -= 1
  • 第四种:
*****
 ****
  ***
   **
    *
i = 1
while i <= 5:
    j = 1
    while j < i:
        print(" ",end='')
        j += 1
    while j >= i and j <= 5:
        print("*",end='')
        j += 1
    print()
    i += 1

版权声明:本文为qq_42036824原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。