Intermediate · Nested loops · 116 of 264
Multiplication grid
Print an n by n multiplication grid. Each number takes 4 characters, right-aligned.
Input: n (1 to 12). Output: n lines; row i, column j shows i * j. Tip: f"{x:4d}"
Examples
Input
3
Output
1 2 3 2 4 6 3 6 9
Input
1
Output
1
One more test checks other inputs when you press Check.
Stuck? The idea behind nested loops
A loop inside a loop: for each turn of the outer loop, the inner one runs all the way through. Good for rows and columns.
print(..., end="") stays on the same line; an empty print() after the inner loop moves to the next row.
for row in range(1, 4):
line = []
for col in range(1, 4):
line.append(str(row * col))
print(" ".join(line))Watch for: print(x, end=" ") leaves a space at the end of every line, and the tests see it. Build each line as a list and print " ".join(line): spaces only between.