Python 打印 G 星形图案程序

本文展示了如何使用 for 循环、while 循环和带示例的函数编写 Python 程序来打印字母 G 星形图案。

下面的字母 G 星形图案示例接受用户输入的行数,嵌套的 for 循环会迭代这些行。elif 条件用于在某些位置打印星号以获得字母 G 的星形图案,并跳过其他位置。

rows = int(input("Enter Alphabet G of Stars Rows = "))
print("====The Alphabet G Star Pattern====")

for i in range(rows):
    for j in range(2 * rows):
        if (i == 0 or i == rows - 1) and (j == 0 or j == (2 * rows) - 2):
            print(end=" ")
        elif j == 0 or (i == 0 and j <= rows) or (i == rows // 2 and j > rows // 2) or \
                (i > rows // 2 and j == (2 * rows) - 1) or (i == rows - 1 and j < 2 * rows):
            print("*", end="")
        else:
            print(end=" ")
    print()
Enter Alphabet G of Stars Rows = 9
====The Alphabet G Star Pattern====
 *********        
*                 
*                 
*                 
*    *************
*                *
*                *
*                *
 *************** *

上面的代码打印了字母 G 的星形图案;下面的代码是另一个带有倾斜的版本。

rows = int(input("Enter Alphabet G of Stars Rows = "))
print("====The Alphabet G Star Pattern====")

for i in range(rows):
    for j in range((2 * rows) - 1):
        if (i == 0 or i == rows - 1) and (j == 0 or j == (2 * rows) - 3):
            print(end=" ")
        elif j == 0 or (i == 0 and j <= rows) or (i == rows // 2 and j > rows // 2) or \
                (i > rows // 2 and j == (2 * rows) - 3) or (i == rows - 1 and j < (2 * rows) - 2):
            print("*", end="")
        else:
            print(end=" ")
    print()
Python Program to Print Alphabetical G Star Pattern

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

此程序使用 while 循环迭代字母 G 图案的行,并在每个位置打印星号,而不是使用 for 循环。有关更多星形图案程序 >> 单击此处

rows = int(input("Enter Alphabet G of Stars Rows = "))

n = rows // 2
m = 2 * rows
i = 0
while i < rows:
    j = 0
    while j < m - 1:
        if (i == 0 or i == rows - 1) and (j == 0 or j == m - 3):
            print(end=" ")
        elif j == 0 or (i == 0 and j <= rows) or (i == n and j > n) or \
                (i > n and j == m - 3) or (i == rows - 1 and j < m - 2):
            print("*", end="")
        else:
            print(end=" ")
        j = j + 1
    print()
    i = i + 1
Enter Alphabet G of Stars Rows = 13
 *************           
*                        
*                        
*                        
*                        
*                        
*      ******************
*                      * 
*                      * 
*                      * 
*                      * 
*                      * 
 ********************** 

在此 Python 示例中,我们创建了一个 GPattern 函数,该函数接受行数和要打印的符号或字符,以绘制给定符号的字母 G 图案。

def GPattern(rows, ch):
    m = 2 * rows
    n = rows // 2
    for i in range(rows):
        for j in range(2 * rows):
            if (i == 0 or i == rows - 1) and (j == 0 or j == m - 2):
                print(end=" ")
            elif j == 0 or (i == 0 and j <= rows) or (i == n and j > n) or \
                    (i > n and j == m - 1) or (i == rows - 1 and j < m):
                print('%c' %ch, end='')
            else:
                print(end=" ")
        print()


row = int(input("Enter Alphabet G of Stars Rows = "))
sy = input("Symbol for G Star Pattern = ")
GPattern(row, sy)
Enter Alphabet G of Stars Rows = 11
Symbol for G Star Pattern = $
 $$$$$$$$$$$          
$                     
$                     
$                     
$                     
$     $$$$$$$$$$$$$$$$
$                    $
$                    $
$                    $
$                    $
 $$$$$$$$$$$$$$$$$$$ $