Python 打印 8 字星形图案程序

编写一个 Python 程序,使用 for 循环打印 8 字星形图案。

rows = int(input("Enter 8 Star Pattern Rows = "))

print("====The 8 Star Pattern====")

for i in range(1, rows * 2):
    if i == 1 or i == rows or i == rows * 2 - 1:
        for j in range(1, rows + 1):
            if j == 1 or j == rows:
                print(end = ' ')
            else:
                print('*', end = '')
    else:
        for k in range(1, rows + 1):
            if k == 1 or k == rows:
                print('*', end = '')
            else:
                print(end = ' ')
    print()
Python Program to Print 8 Star Pattern

Python 使用 while 循环打印 8 字星形图案的程序

rows = int(input("Enter Rows = "))

i = 1

while(i < rows * 2):
    if i == 1 or i == rows or i == rows * 2 - 1:
        j = 1
        while(j <= rows):
            if j == 1 or j == rows:
                print(end = ' ')
            else:
                print('*', end = '')
            j = j + 1
    else:
        k = 1
        while(k <= rows):
            if k == 1 or k == rows:
                print('*', end = '')
            else:
                print(end = ' ')
            k = k + 1
    print()
    i = i + 1
Enter Rows = 8

 ****** 
*      *
*      *
*      *
*      *
*      *
*      *
 ****** 
*      *
*      *
*      *
*      *
*      *
*      *
 ****** 

在此Python 示例中,star8Pattern 函数允许指定行和字符,并打印给定字符的 8 字图案。

def star8Pattern(rows, ch):
    for i in range(1, rows * 2):
        if i == 1 or i == rows or i == rows * 2 - 1:
            for j in range(1, rows + 1):
                if j == 1 or j == rows:
                    print(end = ' ')
                else:
                    print('%c' %ch, end = '')
        else:
            for k in range(1, rows + 1):
                if k == 1 or k == rows:
                    print('%c' %ch, end = '')
                else:
                    print(end = ' ')
        print()

rows = int(input("Enter Rows = "))
ch = input("Symbol to use = " )


star8Pattern(rows, ch)
Enter Rows = 10
Symbol to use = $

 $$$$$$$$ 
$        $
$        $
$        $
$        $
$        $
$        $
$        $
$        $
 $$$$$$$$ 
$        $
$        $
$        $
$        $
$        $
$        $
$        $
$        $
 $$$$$$$$