编写一个 Python 程序,使用 for 循环打印空心左侧帕斯卡星形三角形图案。
rows = int(input("Enter Hollow Left Pascals Star Triangle Pattern Rows = "))
print("====Hollow Left Pascals Star Triangle Pattern====")
for i in range(1, rows + 1):
for j in range(rows, i, -1):
print(end = ' ')
for k in range(1, i + 1):
if k == 1 or k == i:
print('*', end = '')
else:
print(end = ' ')
print()
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(end = ' ')
for k in range(rows - 1, i - 1, -1):
if k == rows - 1 or k == i:
print('*', end = '')
else:
print(end = ' ')
print()

此 Python 程序使用 while 循环打印空心左侧帕斯卡星形三角形图案。
rows = int(input("Enter Hollow Left Pascals Star Triangle Pattern Rows = "))
print("====Hollow Left Pascals Star Triangle Pattern====")
i = 1
while(i <= rows):
j = rows
while ( j > i):
print(end = ' ')
j = j - 1
k = 1
while( k <= i):
if k == 1 or k == i:
print('*', end = '')
else:
print(end = ' ')
k = k + 1
print()
i = i + 1
i = 1
while(i <= rows - 1):
j = 1
while(j <= i):
print(end = ' ')
j = j + 1
k = rows - 1
while(k >= i):
if k == rows - 1 or k == i:
print('*', end = '')
else:
print(end = ' ')
k = k - 1
print()
i = i + 1
Enter Hollow Left Pascals Star Triangle Pattern Rows = 7
====Hollow Left Pascals Star Triangle Pattern====
*
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
*
在此 Python 示例中,我们使用 pyHollowLeftPascalsStar 函数显示给定字符的空心左侧帕斯卡三角形。
def pyHollowLeftPascalStar(rows, ch):
for i in range(1, rows + 1):
for j in range(rows, i, -1):
print(end = ' ')
for k in range(1, i + 1):
if k == 1 or k == i:
print('%c' %ch, end = '')
else:
print(end = ' ')
print()
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(end = ' ')
for k in range(rows - 1, i - 1, -1):
if k == rows - 1 or k == i:
print('%c' %ch, end = '')
else:
print(end = ' ')
print()
rows = int(input("Enter Hollow Left Pascals Star Triangle Pattern Rows = "))
ch = input("Symbol to use in Hollow Left Pascals Triangle Pattern = " )
print("====Hollow Left Pascals Triangle Pattern====")
pyHollowLeftPascalStar(rows, ch)
Enter Hollow Left Pascals Star Triangle Pattern Rows = 14
Symbol to use in Hollow Left Pascals Triangle Pattern = #
====Hollow Left Pascals Triangle Pattern====
#
##
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
##
#