LordPol
LordPol

Reputation: 1

Need to make rows and columns

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.

Upvotes: 1

Views: 1157

Answers (2)

wwii
wwii

Reputation: 23763

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

Upvotes: 0

Nejc
Nejc

Reputation: 240

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()

Upvotes: 1

Related Questions