← Back to Nested Loops
πŸ”’

Number Grid

2 of 6

Exercise 1: Number Grid

Print this grid using nested loops:

1 2 3 4
1 2 3 4
1 2 3 4

Starter Code

# Print a 3-row, 4-column number grid # Each row should show: 1 2 3 4 for row in range(___): for col in range(___): print(___, end=" ") print()

Hints

Hint 1 β€” How many rows?

You need 3 rows, so the outer loop should run 3 times: range(3)

Hint 2 β€” What numbers to print?

Each row prints 1, 2, 3, 4. Use range(1, 5) for the inner loop to get those numbers.

Hint 3 β€” How to stay on the same line?

Use print(col, end=" ") to print on the same line, then print() after the inner loop to move to the next line.


Solution

Show Solution
for row in range(3): for col in range(1, 5): print(col, end=" ") print()

How it works:

  • Outer loop runs 3 times (3 rows)
  • Inner loop runs with values 1, 2, 3, 4
  • end=" " keeps everything on one line
  • print() moves to the next line after each row
πŸ’»

Try it yourself

Code: Number Grid

Loading Python runtime…
Python …
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

βœ… Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

βœ… Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit