Python basic algorithm collection (16)-print diamond

Request to print a diamond

The rhombus is actually a symmetrical structure from top to bottom. As long as the upper part is printed out, the lower part of the figure can be printed as long as the upper part is symmetrical.

 

#循环变量计算为♥总数从0开始左、右分开写。
#如♥总数为7个,那么循环变量是-3,4,步长为1。
'''
需要打印9层最终形成菱形
'''
for j in range(-4,5,1):#从-4开始数到4一共是9个数字
    if j<0:#如果循环变量小于0
        i=-j#让负数变成正数,并重新赋值
    else:
        i=j
    print(' '*i+'*'*(9-2*i))#打印空格+几个⭐

 

Guess you like

Origin blog.csdn.net/weixin_43115314/article/details/114355649