编写一个 Python 程序,使用 for 循环打印 W 星形图案。在此代码中,printStars 函数进行迭代并显示星号,printSpaces 打印空格以打印 W 形状。
def printStars(rows):
for i in range(rows):
print('*', end = '')
def printSpaces(rows):
for i in range(rows):
print(end = ' ')
rows = int(input("Enter W Star Pattern Rows = "))
print("====The W Star Pattern====")
for i in range(rows):
printStars(i + 1)
printSpaces(rows - i - 1)
printStars(rows - i + 1)
printSpaces(2 * i)
printStars(rows - i)
printSpaces(rows - i - 1)
printStars(i + 1);
print()

在此示例中,这两个函数都允许输入任何字符,并使用 while 循环打印给定字符的 W 图案。
def printStars(rows, ch):
i = 0
while(i < rows):
print('%c' %ch, end = '')
i = i + 1
def printSpaces(rows):
i = 0
while(i < rows):
print(end = ' ')
i = i + 1
rows = int(input("Enter W Star Pattern Rows = "))
ch = input("Enter Character = ")
i = 0
while(i < rows):
printStars(i + 1, ch)
printSpaces(rows - i - 1)
printStars(rows - i + 1, ch)
printSpaces(2 * i)
printStars(rows - i, ch)
printSpaces(rows - i - 1)
printStars(i + 1, ch);
print()
i = i + 1
Enter W Star Pattern Rows = 14
Enter Character = #
# ############################# #
## ############## ############# ##
### ############# ############ ###
#### ############ ########### ####
##### ########### ########## #####
###### ########## ######### ######
####### ######### ######## #######
######## ######## ####### ########
######### ####### ###### #########
########## ###### ##### ##########
########### ##### #### ###########
############ #### ### ############
############# ### ## #############
################ ###############