Editor

Fix the Coordinate Grid

Intermediate · Fix the bug · from Nested Loops: Patterns, Performance, and When to Avoid Them

From the lesson: Nested Loops: Patterns, Performance, and When to Avoid Them

Think about a spreadsheet. It has rows and columns. To read every cell, you go through each row, and for each row, you go through each column. That's two loops - one inside the other. That's a nested loop.

Read the lesson

This code is supposed to print a 3x3 coordinate grid, but it has a bug. The output should show each coordinate on its own line, with x going from 0 to 2 and y going from 0 to 2.

Expected output:

(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)

Find and fix the bug.

for x in range(3):
for y in range(3):
print(f'({x},{y})', end=' ')
print()

More Nested Loops exercises

All Python exercises · 50 practice problems · Control Flow quiz