Python 打印字母 V 星形图案程序

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

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

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

for i in range(rows):
    for j in range(2 * rows):
        if (i + j == 2 * rows - 2 or i == j) and i < 2 * rows / 2:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()
Python Program to Print Alphabet V Star Pattern

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

该程序使用 while 循环来迭代字母 V 图案的行,并在某些位置打印星号,而不是使用 For 循环。有关更多星形图案程序 >> 点击这里

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

i = 0
while i < rows:
    j = 0
    while j < 2 * rows:
        if (i + j == 2 * rows - 2 or i == j) and i < 2 * rows / 2:
            print("*", end=" ")
        else:
            print(" ", end=" ")
        j += 1
    print()
    i += 1
Enter Alphabet V of Stars Rows = 10
====The Alphabetical V Star Pattern====
*                                   *   
  *                               *     
    *                           *       
      *                       *         
        *                   *           
          *               *             
            *           *               
              *       *                 
                *   *                   
                  *    

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

def VPattern(rows, ch):
    for i in range(rows):
        for j in range(rows):
            if (i + j == rows - 1 and i < rows / 2) or (i == j and i < rows / 2):
                print('%c' %ch, end=' ')
            else:
                print(" ", end=" ")
        print()


row = int(input("Enter Alphabet V of Stars Rows = "))
sy = input("Symbol for V Star Pattern = ")
VPattern(row * 2 + 1, sy)
Enter Alphabet V of Stars Rows = 12
Symbol for V Star Pattern = #
#                                               # 
  #                                           #   
    #                                       #     
      #                                   #       
        #                               #         
          #                           #           
            #                       #             
              #                   #               
                #               #                 
                  #           #                   
                    #       #                     
                      #   #                       
                        #