使用for循环编写Python程序来打印空心镜像菱形星形图案。此Python示例使用嵌套的for循环和if-else来返回空心镜像菱形图案。
# Python Program to Print Hollow Mirrored Rhombus Star Pattern
rows = int(input("Enter Hollow Mirrored Rhombus Pattern Rows = "))
print("Hollow Mirrored Rhombus Star Pattern")
for i in range(0, rows):
for j in range(0, i):
print(' ', end = '')
for k in range(0, rows):
if(i == 0 or i == rows - 1 or k == 0 or k == rows - 1):
print('*', end = '')
else:
print(' ', end = '')
print()

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