编写一个 Python 程序,使用 For 循环和 While 循环打印交替行的 1 和 0,并附带示例。
Python 程序打印交替行的 1 和 0 使用 For 循环
此 Python 程序允许用户输入总行数和列数。接下来,我们使用 Python 嵌套 For 循环,直到达到用户指定的行和列,打印交替行的 1 和 0。
# Python Program to Print 1 and 0 in alternative rows
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Print Number Pattern - 1 and 0 in alternative rows")
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if(i % 2 != 0):
print('1', end = ' ')
else:
print('0', end = ' ')
print()

Python 程序使用 While 循环显示交替行的 1 和 0
在此 Python 程序中,我们将 For 循环替换为 While 循环
# Python Program to Print 1 and 0 in alternative rows
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Print Number Pattern - 1 and 0 in alternative rows")
i = 1
while(i <= rows):
j = 1
while(j <= columns):
if(i % 2 != 0):
print('1', end = ' ')
else:
print('0', end = ' ')
j = j + 1
i = i + 1
print()
Please Enter the total Number of Rows : 12
Please Enter the total Number of Columns : 18
Print Number Pattern - 1 and 0 in alternative rows
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>>
Python 程序在没有 If 的情况下显示交替行的 1 和 0
在此程序中,我们在打印语句中查找奇偶行。通过这种方式,您可以避免在 Python 嵌套 For 循环中使用 If 语句。
# Python Program to Print 1 and 0 in alternative rows
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Print Number Pattern - 1 and 0 in alternative rows")
for i in range(1, rows + 1):
for j in range(1, columns + 1):
print('%d' %(i % 2), end = ' ')
print()
Please Enter the total Number of Rows : 8
Please Enter the total Number of Columns : 12
Print Number Pattern - 1 and 0 in alternative rows
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
>>>
Python 程序显示交替行的 0 和 1
如果您想在交替行中打印 0 和 1,请将打印语句中的 1 替换为 0,将 0 替换为 1。
# Python Program to Print 1 and 0 in alternative rows
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Print Number Pattern - 0 and 1 in alternative rows")
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if(i % 2 != 0):
print('0', end = ' ')
else:
print('1', end = ' ')
print()
Please Enter the total Number of Rows : 10
Please Enter the total Number of Columns : 15
Print Number Pattern - 0 and 1 in alternative rows
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>>>