使用 For 循环和 While 循环编写 Python 程序,打印数字 1 和 0 的空心框图案,并附有示例。
Python 使用 For 循环打印数字 1 和 0 的空心框图案程序
此 Python 程序允许用户输入总行数和列数。接下来,我们使用 Python 嵌套 For 循环来迭代每一行和每一列的项。在循环中,我们使用 If 语句来检查行号和列号是否为 1 或最大值。如果为真,则 Python 打印 1,否则打印空格。
# Python Program to Print Hollow Box Pattern of Numbers 1 and 0
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Hollow Box Pattern of Numbers")
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if(i == 1 or i == rows or j == 1 or j == columns):
print('1', end = ' ')
else:
print(' ', end = ' ')
print()

Python 使用 While 循环显示数字 1 和 0 的空心框图案程序
此 Python 程序用于显示空心框图案,与上面的程序相同。但是,我们将 For 循环替换为 While 循环。
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Hollow Box Pattern of Numbers")
i = 1
while(i <= rows):
j = 1;
while(j <= columns ):
if(i == 1 or i == rows or j == 1 or j == columns):
print('1', end = ' ')
else:
print(' ', end = ' ')
j = j + 1
i = i + 1
print()
Please Enter the total Number of Rows : 10
Please Enter the total Number of Columns : 17
Hollow Box Pattern of Numbers
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>>>
Python 显示数字 0 和 1 的空心框图案程序
如果您想打印数字 0 和 1 的空心框图案,请将打印语句中的 1 替换为空格,并将空格替换为 1。
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Hollow Box Pattern of Numbers")
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if(i == 1 or i == rows or j == 1 or j == columns):
print('0', end = ' ')
else:
print(' ', end = ' ')
print()
Please Enter the total Number of Rows : 12
Please Enter the total Number of Columns : 15
Hollow Box Pattern of Numbers
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>>