Search code examples
pythonfor-looptabular

Table in python


In python I tried to make table like this

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

I tried this code:

for i in range(1, 11):
    for j in range(1,10):
        print(j, end=" ")
    print(i)

but it outputs:

1 2 3 4 5 6 7 8 9 1
1 2 3 4 5 6 7 8 9 2
1 2 3 4 5 6 7 8 9 3
1 2 3 4 5 6 7 8 9 4
1 2 3 4 5 6 7 8 9 5
1 2 3 4 5 6 7 8 9 6
1 2 3 4 5 6 7 8 9 7
1 2 3 4 5 6 7 8 9 8
1 2 3 4 5 6 7 8 9 9
1 2 3 4 5 6 7 8 9 10

Solution

  • Your outer loop is only used to count the number of rows that will be printed. Seems like you want a 10x10 table, so you have 10 rows.

    for row in range(10):
    

    Then you want the numbers 1 through 10 printed on each row:

    for column in range(1,11):
        print(column, end=' ')
    

    Then you want to write a newline so that each row shows up on a new line.

    print()
    

    That should do it.