Home > other >  Need to make rows and columns
Need to make rows and columns

Time:02-06

nRow=int(input("No. of rows: "))
nCol=int(input("No. of columns: "))
ch=input("Which characters? ") 
i=1 
j=1 
while j<=nCol: 
    while i<=nRow: 
        print(ch, end='') 
        i=i 1 
    print(ch, end='') 
    j=j 1

Here I tried my best to make a column and row in while loops, but it just didn't work out. When I input row and column numbers, they just gin one row. Like (Rows:2 Columns:3 characters:a), I expected to get like a table with 2 rows and 3 columns, but I got just - aaaaa.

CodePudding user response:

You just need to change your loop so you go to new line after every row. You can also use for in range like this

for row in range(nRow):
    for col in range(nCol):
        print(ch, end=' ')
    print()

CodePudding user response:

Put rows in the outer loop and columns in the inner loop. At the end of each row reset the column counter and print a newline.

while j<=nRow: 
    while i<=nCol: 
        print(ch, end='') 
        i=i 1 
    i = 1
    print() 
    j=j 1
  • Related