Bootstrap

【Python】简单输出一些图案

目录

风车

样式

代码

菱形

样式

代码

X型

样式

代码

K型

样式

代码

箭型

样式

 代码

等边三角形

样式

 代码

空心正方形

样式 

代码

空心直角三角形

样式

代码

数字三角形

形状

代码

圣诞树

形状

代码

超级圣诞树

形状

代码


风车

样式

代码

思路一:分成五个部分 最上边一行,中间一行,最下边一行,中间剩余的两个部分

思路二:两层循环,满足条件,输出*,否则输出空格

while True:
    try:
        n = int(input())
        print("*" + " " * (n - 2) + "*" * n)
        for i in range(n - 2):
            print("*" + " " * i + "*" + " " * (n - 3 - i) + "*" + (n - 3 - i) * " " + "*")
        print("*" * (2 * n - 1))
        for i in range(n - 2):
            print(" " * (n - 2 - i) + "*" + " " * i + "*" + " " * i + "*" + (n - 3 - i) * " " + "*")
        print(n * "*" + (n - 2) * ' ' + "*")
    except:
        break

菱形

样式

代码

while True:
    try:
        n = int(input())
        for i in range(1, n + 1):
            print(" " * (n - i + 1) + "* " * i)
        for i in range(n + 1, 0, -1):
            print(" " * (n - i + 1) + "* " * i)
    except:
        break

X型

样式

代码

while True:
    try:
        n = int(input())
        for i in range(n):
            for j in range(n):
                if i == j or i + j + 1 == n:
                    print('*', end="")
                else:
                    print(' ', end="")
            print()
    except:
        break

K型

样式

代码

while True:
    try:
        n = int(input())
        for i in range(n + 1, 0, -1):
            print("* " * i + " " * (n - i + 1))
        for i in range(2, n + 2):
            print("* " * i)
    except:
        break

箭型

样式

 代码

while True:
    try:
        n = int(input())
        for i in range(n + 1, 0, -1):
            print(" " * 2 * (i - 1) + "*" * (n + 2 - i))
        for i in range(n, 0, -1):
            print(" " * 2 * (n - i + 1) + "*" * i)
    except:
        break

等边三角形

样式

 代码

while True:
    try:
        n = int(input())
        for i in range(n + 1, 0, -1):
            print(" " * 2 * (i - 1) + "*" * (n + 2 - i))
        for i in range(n, 0, -1):
            print(" " * 2 * (n - i + 1) + "*" * i)
    except:
        break

空心正方形

样式 

代码

while True:
    try:
        n = int(input())
        print("* " * n)
        for i in range(n - 2):
            print("* " + " " * 2 * (n - 2) + "* ")
        print("* " * n)
    except:
        break

空心直角三角形

样式

代码

while True:
    try:
        n = int(input())
        for i in range(1, n + 1):
            for j in range(1, n + 1):
                if i == j or j == 1 or i == n:
                    print('* ', end='')
                else:
                    print('  ', end='')
            print()
    except:
        break

数字三角形

形状

代码

while True:
    try:
        n = int(input())
        for i in range(1, n + 1):
            for j in range(1, i + 1):
                print(f"{j} ", end="")
            print()
    except:
        break

圣诞树

形状

代码

a = "  *   "
b = " * *  "
c = "* * * "
d = " " * 3
while True:
    try:
        n = int(input())
        for i in range(1, n + 1):
            print(d * (n - i) + a * i)
            print(d * (n - i) + b * i)
            print(d * (n - i) + c * i)
        w = 6 * n - 1
        for i in range(n):
            print("*".center(w))
    except:
        break

超级圣诞树

形状

代码

while True:
    try:
        h = int(input())
        l = ["*", "* *", "* * *"]
        for i in range(h - 1):
            n = len(l)
            for j in range(n):
                l.append(l[j] + " " * (2 * n - 1 - 2 * j) + l[j])
        w = 6 * 2 ** (h - 1) - 1
        for i in range(len(l)):
            print(l[i].center(w))
        for i in range(h):
            print("*".center(w))
    except:
        break

源码在这:https://github.com/harbinailin/Python/tree/master/%E7%BC%96%E7%A8%8B%E5%88%9D%E5%AD%A6%E8%80%85/%E5%BE%AA%E7%8E%AF%E8%BE%93%E5%87%BA%E5%9B%BE%E5%BD%A2

;