<- Back to the chapter.

Answer to Problem 6

Question:

Write code that will print the following:

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

Use two nested loops. Print the first line with a loop, and not with:

print("0 1 2 3 4 5 6 7 8 9")

Tip: First, create a loop that prints the first line. Then enclose it in another loop that repeats the line 10 times. Use the i or j variables. This example, and the next one, helps reinforce what those index variables are doing.

Answer:

for row in range(10):
    for column in range(10):
        print(column,end=" ")

    # Print a blank line to move to the next row
    print()
Variables:
row=
column=
Output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

Explanation:

Video: Answer to Problem 6