用python画如下菱形,前四行一个规律,后三行一个规律,for循环使用
1 s = '*' 2 for i in range(1,8,2): 3 print('{:^7}'.format((s*i))) #格式化字符串方法一 4 for i in reversed(range(1,6,2)): 5 print((s*i).center(7)) #格式化字符串方法二
方法二:自行手动输入行:经分析,菱形只有当数据为奇数时打印出来得好看
1 n = int(input("输入数值:")) 2 if n % 2 != 0: 3 str1 = "*" 4 for j in range(1,n,2): 5 print((str1*j).center(n)) 6 for x in range(n,0,-2): 7 print((str1*x).center(n)) 8 else: 9 print("请输入奇数")
执行结果:
输入数值:7 * *** ***** ******* ***** *** *