Home > database >  how can i write a python code for give row and column and print 10101 and 01010 pattern
how can i write a python code for give row and column and print 10101 and 01010 pattern

Time:01-21

i want to write a python code for give row and column from input and print a pattern like this photo:
enter image description here

i write this code but this just show 10101 :

rows = int(input("Please Enter the total Number of Rows  : "))
columns = int(input("Please Enter the total Number of Columns  : ")) 
for i in range(1, rows   1):
for j in range(1, columns   1):
    if(j % 2 == 0):          
        print('0', end = '  ')
    else:
        print('1', end = '  ')
print()

thanks for your attention...☺♥

CodePudding user response:

You need to consider both column and row index for correct solution.

rows = int(input("Please Enter the total Number of Rows  : "))
columns = int(input("Please Enter the total Number of Columns  : ")) 
for i in range(1, rows   1):
    for j in range(1, columns   1):
        if((j   i) % 2 == 1):          
            print('0', end = '  ')
        else:
            print('1', end = '  ')
    print()

Or shorter version

for i in range(1, rows   1):
    for j in range(1, columns   1):
            print((i   j   1)%2, end = '  ')
    print()
  •  Tags:  
  • Related