编写一个 Python 程序,使用 for 循环打印加号星型图案。在此 Python 示例中,两个 for 循环都将从 1 遍历到 2 * rows。if 语句会检查 i 或 j 是否等于 rows,如果条件有效,则打印星号。
# Python Program to Print Plus Star Pattern
rows = int(input("Enter Plus Pattern Rows = "))
print("Plus Star Pattern")
for i in range(1, 2 * rows):
for j in range(1, 2 * rows):
if i == rows or j == rows:
print('*', end = '')
else:
print(' ', end = '')
print()

Python 使用 while 循环打印加号星型图案的程序。
# Python Program to Print Plus Star Pattern
rows = int(input("Enter Plus Pattern Rows = "))
print("Plus Star Pattern")
i = 1
while(i < 2 * rows):
j = 1
while(j < 2 * rows):
if i == rows or j == rows:
print('*', end = '')
else:
print(' ', end = '')
j = j + 1
i = i + 1
print()
Enter Plus Pattern Rows = 7
Plus Star Pattern
*
*
*
*
*
*
*************
*
*
*
*
*
*
>>>
在此 Python 程序中,plusPattern 函数打印给定符号的加号图案。
# Python Program to Print Plus Star Pattern
def plusPattern(rows, ch):
for i in range(1, 2 * rows):
for j in range(1, 2 * rows):
if i == rows or j == rows:
print('%c' %ch, end = '')
else:
print(' ', end = '')
print()
rows = int(input("Enter Plus Pattern Rows = "))
ch = input("Symbol to use in Plus Pattern = " )
print("Plus Pattern")
plusPattern(rows, ch)
Enter Plus Pattern Rows = 8
Symbol to use in Plus Pattern = $
Plus Pattern
$
$
$
$
$
$
$
$$$$$$$$$$$$$$$
$
$
$
$
$
$
$
>>>