编写一个Python程序,使用for循环打印空心半菱形星形图案。if语句检查i是否等于j,或者j是否等于零或一,如果为真则打印星号;否则打印空格。
# Python Program to Print Hollow Half Diamond Star Pattern
rows = int(input("Enter Hollow Half Diamond Pattern Rows = "))
print("Hollow Half Diamond Star Pattern")
for i in range(0, rows):
for j in range(0, i + 1):
if i == j or j == 0:
print('*', end = '')
else:
print(' ', end = '')
print()
for i in range(rows - 1, 0, -1):
for j in range(1, i + 1):
if i == j or j == 1:
print('*', end = '')
else:
print(' ', end = '')
print()

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