python|两种使用python打印菱形的方法


1、使用center方法

s = '*' for i in range(1, 8, 2): print((s*i).center(7)) for i in reversed(range(1, 6, 2)): print((s*i).center(7))



2、使用普通方法

#coding:utf-8 rows = int(input('输入列数: ')) i = j = k = 1 #声明变量,i用于控制外层循环(图形行数),j用于控制空格的个数,k用于控制*的个数 #打印菱形 print("打印空心等菱形,这里去掉if-else条件判断就是实心的") for i in range(rows): for j in range(rows - i): print(" ", end=" ") j += 1 for k in range(2 * i - 1): if k == 0 or k == 2 * i - 2: print("*", end=" ") else: print(" ", end=" ") k += 1 print ("\n") i += 1 #菱形的下半部分 for i in range(rows): for j in range(i): #(1,rows-i) print(" ", end=" ") j += 1 for k in range(2 * (rows - i) - 1): if k == 0 or k == 2 * (rows - i) - 2: print("*", end=" ") else: print(" ", end=" ") k += 1 print("\n") i += 1



    推荐阅读