编写一个Python程序,使用for循环来打印W星形图案。在这段Python代码中,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()
在这个Python例子中,两个函数都允许输入任何字符,并使用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 = #
# ############################# #
## ############## ############# ##
### ############# ############ ###
#### ############ ########### ####
##### ########### ########## #####
###### ########## ######### ######
####### ######### ######## #######
######## ######## ####### ########
######### ####### ###### #########
########## ###### ##### ##########
########### ##### #### ###########
############ #### ### ############
############# ### ## #############
################ ###############