使用 for 循环编写一个 Python 程序来打印空心菱形星形图案。嵌套的 for 循环和 if-else 语句有助于打印空心菱形图案。
hrows = int(input("Enter Howllow Rhombus Star Pattern rows = "))
print("Hollow Rhombus Star Pattern")
for i in range(hrows, 0, -1):
for j in range(1, i):
print(' ', end = '')
for k in range(0, hrows):
if(i == 1 or i == hrows or k == 0 or k == hrows - 1):
print('*', end = '')
else:
print(' ', end = '')
print()

此程序使用 while 循环打印空心菱形星形图案。
hrows = int(input("Enter Howllow Rhombus Star Pattern rows = "))
print("Hollow Rhombus Star Pattern")
i = hrows
while(i >= 1):
j = 1
while(j <= i - 1):
print(' ', end = '')
j = j + 1
k = 1
while(k <= hrows):
if(i == 1 or i == hrows or k == 1 or k == hrows):
print('*', end = '')
else:
print(' ', end = '')
k = k + 1
i = i - 1
print()
Enter Howllow Rhombus Star Pattern rows = 15
Hollow Rhombus Star Pattern
***************
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
***************
>>>
在此 Python 示例中,我们创建了一个 hollowRhombusStar 函数来打印空心菱形图案。它用给定的符号替换空心菱形图案的星号。
def hollowRhombusStar(hrows, ch):
for i in range(hrows, 0, -1):
for j in range(1, i):
print(' ', end = '')
for k in range(0, hrows):
if(i == 1 or i == hrows or k == 0 or k == hrows - 1):
print('%c' %ch, end = '')
else:
print(' ', end = '')
print()
hrows = int(input("Enter hrows = "))
ch = input("Symbol to use in Howllow Rhombus Pattern = " )
print("Hollow Rhombus Star Pattern")
hollowRhombusStar(hrows, ch)
Enter hrows = 18
Symbol to use in Howllow Rhombus Pattern = #
Hollow Rhombus Star Pattern
##################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
##################
>>>