if与while相互嵌套,菱形*的实现.py

"""
    *        
   * *       
  * * *      
 * * * *     
* * * * *    
 * * * *     
  * * *      
   * *       
    *        
"""

# -*-coding:utf-8-*-

# i = 0
#
# while i < 9:
#
#     if i < 5:
#
#         print(" "*(4-i),"* "*(i+1))
#
#     else:
#
#         print(" "*(i-4),"* "*(9-i))
#
#     i += 1


"""
    *        1 4 1
   * *       2 3 2
  * * *      3 2 3
 * * * *     4 1 4
* * * * *    5 0 5
 * * * *     6 1 4
  * * *      7 2 3
   * *       8 3 2
    *        9 4 1
"""

row = 1

 

while row <= 9:

    if row <= 5:

        print(" "*(5-row),end="")

        col = 1

        while col <= row:


            print("* ",end="")

            col += 1

        print("")

    else:

        print(" "*(row-5),end="")

        col = 1

        while col <= (10 -row):

 

            print("* ",end="")


            col += 1
        print("")

    row += 1

posted @ 2018-01-10 21:36  BaTi_LDY  阅读(657)  评论(0编辑  收藏  举报