python for循环打印菱形

  • 首先要学会打印等腰三角形如下:
rows = int(input("请输入一个正整数:"))#输入打印的行数
for i in range(0,rows):#外层循环
    for j in range(1,rows-i):#循环输出空格
        print(" ",end='')
    for a in range (0,i*2+1):#循环输出*
        print("*",end='')
    print()
  • 打印菱形如下:
choose = int(input("请选择菱形类型:1偶数行,2奇数行"))#选择类型
if choose == 1:
    rows = int(input("请输入一个偶数:"))
    for i in range(0,rows//2):
        for j in range (0,rows//2-i-1):
            print(" ",end='')
        for a in range(0,i*2+1):
            print("*",end='')
        print()
    for i in range(0,rows//2):
        for j in range (0,i):
            print(" ",end='')
        for a in range(0,2*(rows//2-1-i)+1):
            print("*",end='')
        print()
elif choose == 2:
    rows = int(input("请输入一个奇数:"))
    for i in range(0, rows // 2+1):
        for j in range(1, rows // 2+1-i ):
            print(" ", end='')
        for a in range(0, i * 2 + 1):
            print("*", end='')
        print()
    for i in range(0, rows // 2):
        for j in range(0, i+1):
            print(" ", end='')
        for a in range(0, 2 * (rows // 2 - 1 - i)+1):
            print("*", end='')
        print()

你可能感兴趣的:(学习心得)