1. Outer countdown.i runs from 10 down to 1 — start of the left half (or empty when i = 10).
2. Left then center. Print j from i to 9, then print "0".
3. Right mirror. Print k from 9 down to i; then print() ends the row.
Example 2 — input() Max Digit
Read the max digit at runtime; clamp to 1–9 and start the outer loop at max_n + 1.
Python
max_n = int(input("Enter max digit (1-9): "))
if max_n < 1:
max_n = 1
if max_n > 9:
max_n = 9
for i in range(max_n + 1, 0, -1):
for j in range(i, max_n + 1):
print(j, end="")
print("0", end="")
for k in range(max_n, i - 1, -1):
print(k, end="")
print()
Output (when user enters 4)
Enter max digit (1-9): 4
0
404
34043
2340432
123404321
How It Works
1. Read and clamp. Parse the answer; keep max_n in 1–9.
2. Generalize bounds. Outer starts at max_n + 1; both halves use max_n instead of 9.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter max digit (1-9): ").strip()
try:
max_n = int(raw)
except ValueError:
print("Enter a digit from 1 to 9.")
raise SystemExit(1)
if max_n < 1 or max_n > 9:
print("Enter a digit from 1 to 9.")
raise SystemExit(1)
Example 3 — Spaced Digits
Keep max_n = 5 but print a space after each digit (and after 0).
Python
max_n = 5
for i in range(max_n + 1, 0, -1):
for j in range(i, max_n + 1):
print(j, end=" ")
print("0", end=" ")
for k in range(max_n, i - 1, -1):
print(k, end=" ")
print()
1. Same structure. Outer countdown and both half-loops match Example 2.
2. Only end= changes. Use end=" " for left digits, center 0, and right digits.
3. Same shape. Values and row count stay the same — only spacing differs.
Edge Cases & Pitfalls
Check these before calling the solution done.
no 0
Missing center
Forgetting print("0", end="") glues left and right halves together without the zero pivot.
i = max
Wrong outer start
Starting at i = max_n skips the lone 0 row. Use range(max_n + 1, 0, -1).
print() inside
Vertical digits
If print() is inside either half-loop, each digit lands on its own line. Call it only after all three parts.
max_n = 1
Smallest mirror
Output is 0 then 101.
max > 9
Multi-digit cells
Values above 9 print as two characters and break the single-digit look. Clamp to 9 in input examples.
input()
Catch ValueError
Letters crash bare int(input()) — wrap in try/except and require 1–9.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(1)
About n + 1 rows, each printing up to 2n + 1 digits → O(n²).
Remember
Key Takeaways
Rule: print i..max, then 0, then max..i.
Start at max+1: first row is only the center zero.
end= vs print(): digits stay on the line; print() advances after each row.
Complexity:O(n²) for max digit n.
One line: grow both sides toward the max digit while keeping a fixed 0 in the middle of every row.
Frequently Asked Questions
print("0", end="") sits between the ascending and descending loops, creating a fixed center on every row.
When i = max + 1, both side loops are empty — only 0 is printed.
Starting one past the max digit makes the first row have empty left and right halves, giving the single 0 row.
Program 27 mirrors 1..i on each row. Program 28 uses a fixed 0 center and grows digits toward max on both sides as i decreases.
Replace 9 with max_n and start i at max_n + 1 — see Example 2.
Use print(j, end=" ") and print(k, end=" ") (and print("0 ", end="")) — see Example 3.
O(n²) for max digit n because each row prints O(n) digits and there are O(n) rows.
Use try/except ValueError around int(input()) and clamp max_n to 1..9 so loop bounds stay valid.
Two rows: 0 and 101 — the smallest non-trivial mirror with a zero center.
🤔
Did you know?
This pattern prints ascending digits from i to max, a fixed 0 in the center, then descending digits from max down to i. As i decreases, each row grows into the long mirror 1234567890987654321.