编写一个 Python 程序,使用嵌套的 for 循环打印倒金字塔星形图案。
rows = int(input("Enter Inverted Pyramid Pattern Rows = "))
print("Inverted Pyramid Star Pattern")
for i in range(rows, 0, -1):
for j in range(0, rows - i):
print(end = ' ')
for k in range(0, i):
print('*', end = ' ')
print()

此程序使用 while 循环打印倒金字塔星形图案。
rows = int(input("Enter Inverted Pyramid Pattern Rows = "))
print("Inverted Pyramid Star Pattern")
i = rows
while(i >= 1):
j = 0
while(j <= rows - i):
print(end = ' ')
j = j + 1
k = 0
while(k < i):
print('*', end = ' ')
k = k + 1
i = i - 1
print()
Enter Inverted Pyramid Pattern Rows = 14
Inverted Pyramid Star Pattern
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
在此示例中,我们创建了一个 invertedStarPyramid 函数来打印倒金字塔星形图案。它会将实心倒金字塔星形中的星号替换为给定的符号。
def invertedStarPyramid(rows, ch):
for i in range(rows, 0, -1):
for j in range(0, rows - i):
print(end = ' ')
for k in range(0, i):
print('%c' %ch, end = ' ')
print()
rows = int(input("Enter Inverted Pyramid Pattern Rows = "))
ch = input("Symbol to use in Inverted Pyramid Pattern = ")
print()
invertedStarPyramid(rows, ch)
Enter Inverted Pyramid Pattern Rows = 15
Symbol to use in Inverted Pyramid Pattern = ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^
^ ^ ^ ^
^ ^ ^
^ ^
^
>>>