欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用python画一个菱形

程序员文章站 2022-07-15 10:47:07
...
if n%2==0:
    for i in range(1,n):
        if i<=n/2:
            for j in range(1, abs(n - i) + 1):
                print(end=" ")
            for k in range(1,2*i):
                print("*",end="")
        else:
            a=n/2
            b=i-a
            i=a-b
            j=1
            while j<abs(n+1-i):
                print(end=" ")
                j+=1
            k=1
            while k<2*i:
                print("*",end="")
                k+=1
        print(" ")
else:
    for i in range(1,n):
        if i<=(n-1)/2:
            for j in range(1, abs(n - i) + 1):
                print(end=" ")
            for k in range(1,2*i):
                print("*",end="")
        else:
            a=(n-1)/2
            b=i-a
            i=a-b+1
            j=1
            while j<abs(n+1-i):
                print(end=" ")
                j+=1
            k=1
            while k<2*i:
                print("*",end="")
                k+=1
        print(" ")