用Python程序打印倒置的V字形图案

174 阅读1分钟

编写一个Python程序,使用for循环打印倒V型星形图案或方形星形图案内的半钻石。

rows = int(input("Enter Inverted V Star Pattern Rows = "))

print("====The Inverted V Star Pattern====")

for i in range(rows, 0, -1): for j in range(1, i + 1): print('\*', end = '') for k in range(1, 2 \* (rows - i) + 1): print(end = '') for l in range(1, i + 1): print('\*', end = '' ) print()

image.png

这个Python程序使用while循环来显示星星的倒V星模式。

rows = int(input("Enter Inverted V Star Pattern Rows = ")

print("====The Inverted V Star Pattern====") i = rows

while(i >= 1): j = 1 while(j <= i): print('\*', end = '') j = j + 1 k = 1 while(k <= 2 \* (rows - i)): print(end = '' ) k = k + 1 l = 1 while(l <= i): print('\*', end = '') l = l + 1 print() i = i - 1
Enter Inverted V Star Pattern Rows = 9
====The Inverted V Star Pattern====
******************
********  ********
*******    *******
******      ******
*****        *****
****          ****
***            ***
**              **
*                *

在这个Python模式的例子中,我们创建了一个函数,允许输入任何字符,并打印出给定字符的倒置V。

def InvertedVStar(i, ch): for j in range(1, i + 1): print('%c' %ch, end = '' )

rows = int(input("Enter Inverted V Star Pattern Rows = " ))

ch = input("V型图案中使用的符号 = " )

print("====The Inverted V Star Pattern====")

for i in range(rows, 0, -1): InvertedVStar(i, ch) for k in range(1, 2 \* (rows - i) + 1): print(end = ' ' ) InvertedVStar(i, ch) print()
Enter Inverted V Star Pattern Rows = 14
Symbol to use in V Pattern = #
====The Inverted V Star Pattern====
############################
#############  #############
############    ############
###########      ###########
##########        ##########
#########          #########
########            ########
#######              #######
######                ######
#####                  #####
####                    ####
###                      ###
##                        ##
#                          #