Shape Rule
Centered pyramid
Row 1 prints 1, row 2 prints 2 3 4, row 3 prints 5 6 7 8 9 with leading spaces.

The centered continuous number pyramid prints 1, then 2 3 4, then 5 6 7 8 9 — a natural step after the mirror pattern in Program 23. This tutorial covers odd row widths, leading spaces, a running counter k, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
Centered pyramid
Row 1 prints 1, row 2 prints 2 3 4, row 3 prints 5 6 7 8 9 with leading spaces.
i += 2
for i in range(1, max_n + 1, 2) sets odd row widths 1, 3, 5.
if j > i
Reverse loop prints spaces first, then k += 1 for each number slot.
k never resets
k = 1 before the outer loop; k += 1 continues across rows.
Odd widths 1–9
Pick a max odd width and draw the centered pyramid instantly in the browser.
Complexity
Each row scans max_n columns; total work scales as n².
A centered continuous number pyramid prints numbers that keep counting across rows, with leading spaces to center each row. With max width 5, the output is 1, 2 3 4, 5 6 7 8 9 (spaces shown in the worked examples below).
In Python you use an outer loop with odd widths, a reverse inner loop with an if for spaces vs k += 1, then print() ends each row.
It combines spacing logic with a persistent counter — a step up from Program 23’s three inner loops.
i = 1, 3, 5 controls how many numbers print per row.
if j > i prints spaces before numbers.
k += 1 never resets — numbers flow across rows.
Follow Program 23; continue to Program 25 (bidirectional triangle) next.
In short: for each odd i, scan j from max_n down to 1 — print a space when j > i, else print k then k += 1, then print().
Given a positive odd max width (e.g. 5), print a centered pyramid where numbers increase continuously across rows using a counter k.
# max_n = 5 (conceptual shape — dots show spaces)
# ··1·
# ·2·3·4
# 5·6·7·8·9 | Item | Type | Description |
|---|---|---|
max_n | int | Maximum odd row width — inner loop scans j from max_n down to 1. |
i | int | Outer loop — odd row widths 1, 3, 5 via range(..., 2). |
j | int | Reverse inner loop — spaces when j > i, else print number. |
k | int | Running counter — starts at 1, increments with k += 1 across all rows. |
k = 1
for i from 1 to max_n step 2:
for j from max_n down to 1:
if j > i:
print space
else:
print k; k = k + 1
print newline | Approach | Idea | Best for |
|---|---|---|
| Spacing + counter | 1, 2 3 4, 5 6 7 8 9 | Learning and interviews |
| User-input max | max_n = int(input(...)) | Flexible console programs |
| Safe input | try/except ValueError loop + even-width adjustment | Robust user-facing demos |
| Goal | Pattern |
|---|---|
| Walk rows | for i in range(1, max_n + 1, 2) |
| Init counter | k = 1 before the outer loop |
| Scan columns | for j in range(max_n, 0, -1) |
| Space or number | if j > i: print(" ", end="") else: print(k, end=" "); k += 1 |
| End the row | print() |
| User input | max_n = int(input(...)) |
Same centered pyramid — different ways to control width and input validation.
range(..., 2)Odd row widths 1, 3, 5
j > iLeading spaces center each row
k += 1Numbers continue across rows
if/elseOne inner loop handles space vs number
Reach for this pattern when teaching centering with spaces, persistent counters, and if/else inside nested loops.
Natural follow-up after Program 23 — introduces spacing logic and a running counter.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible row count.
Compare Program 23 (mirror pattern) and Program 25 (bidirectional triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose an odd max width between 1 and 9 and draw the centered continuous pyramid in the browser.
Three complete Python programs — fixed max width, user input, and safe input with validation. Click View Output to reveal sample console results.
Print three rows of the centered pyramid with a running counter.
max_n = 5Hard-coded width — ideal for first demos and screenshots.
k = 1
for i in range(1, 6, 2):
for j in range(5, 0, -1):
if j > i:
print(" ", end="")
else:
print(k, end=" ")
k += 1
print() When i = 1, print two spaces then 1 — output 1. When i = 3, print one space then 2 3 4. When i = 5, print 5 6 7 8 9 with no leading spaces. k never resets, so numbers continue across rows.
Read the maximum odd width with input() instead of hard-coding 5.
Read max_n with input() and int(); adjust even widths to the nearest odd value.
max_n = int(input("Enter the maximum odd width: "))
if max_n % 2 == 0:
max_n -= 1
if max_n < 1:
raise SystemExit
k = 1
for i in range(1, max_n + 1, 2):
for j in range(max_n, 0, -1):
if j > i:
print(" ", end="")
else:
print(k, end=" ")
k += 1
print() Same spacing + counter core as Example 1; only the source of max_n changes. The even-width adjustment keeps row sizes odd for a proper pyramid shape. Non-numeric input raises ValueError from int(input()) — wrap it in try/except in safer labs.
Use try/except ValueError so bad input does not crash the script.
try/except LoopValidate input before drawing the pyramid — prompt again on failure.
max_n = 0
print("Enter the maximum odd width: ", end="")
while max_n < 1:
try:
max_n = int(input())
if max_n < 1:
print("Please enter a positive whole number: ", end="")
except ValueError:
print("Please enter a positive whole number: ", end="")
if max_n % 2 == 0:
max_n -= 1
k = 1
for i in range(1, max_n + 1, 2):
for j in range(max_n, 0, -1):
if j > i:
print(" ", end="")
else:
print(k, end=" ")
k += 1
print() try/except ValueError fails on bad input — the loop re-prompts until a valid positive integer is entered, then the pyramid draws as usual.
print is built in; use input() when reading input. Set k = 1 and loop variables i, j.
for i in range(1, max_n + 1, 2) — row widths 1, 3, 5 grow the pyramid.
for j in range(max_n, 0, -1) scans columns from right to left.
if j > i prints a space; else print(k, end=" ") and k += 1.
print() ends the row after the inner loop.
Numbers continue across rows — O(n²) time, O(1) extra memory.
max_n = 5Trace each outer-loop value of i, leading spaces, numbers printed, and k after each row.
i | Leading spaces | Numbers printed | k after row | Row output |
|---|---|---|---|---|
1 | 2 (when j = 5, 4) | 1 | 2 | 1 |
3 | 1 (when j = 5) | 2, 3, 4 | 5 | 2 3 4 |
5 | 0 | 5, 6, 7, 8, 9 | 10 | 5 6 7 8 9 |
Leading spaces per row = (max - i) / 2 when max_n is odd — centers each row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: put print() inside the inner loop by mistake.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: reset k each row and compare output.
Practice print(..., end=" ") vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print k with end=" " and padded widths for 2-digit numbers.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for max_n = 9 → 1 + 3 + 5 + 7 + 9 = 25.
Pair the pattern with try/except ValueError and positive-width checks.
Example: reject max_n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Python courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace the reverse inner loop on paper for max_n = 3 before coding — spacing bugs hide in the j > i condition.
Small habits that keep number-pattern code clean.
Do not reset k inside the outer loop unless you want per-row numbering.
input()Use try/except ValueError so bad input does not crash the script.
print() OutsideOnly call print() after the inner loop finishes the row.
Write each i, space count, and numbers printed before coding.
Trace max_n = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.
Mistakes that commonly break centered pyramid patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(" ", end="") or print(k, end=" "); k += 1; print() only after the inner loop.
Putting k = 1 inside the outer loop restarts numbering — you lose the continuous effect.
→ Initialize k = 1 once before the outer loop unless you want per-row numbering.
Without if j > i the pyramid is left-aligned, not centered.
→ Print a space when j > i before printing numbers.
Even max_n values break the centering math for this version.
→ Subtract 1 when max_n % 2 == 0, or validate and prompt again.
Letters or empty input raise ValueError from int(input()).
→ Wrap int(input()) in try/except ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just a centered 1 with leading spaces.
Outer loop never runs — print nothing or show a message.
max_n < 0Treat as invalid; re-prompt instead of silent empty output.
Subtract 1 to force odd width, or re-prompt for an odd value.
int(input()) raises ValueError — validate with try/except first.
Two rows: centered 1 and 2 3.
Try these variations to lock in the pattern.
k = 1 inside the outer loopk += 1 on charsj > i shift numbers right — row width stays at max_n columns.print stays on the line; print() advances — mix them carefully.max_n > 0 for interactive programs; max_n = 1 prints a single centered 1.Quick Takeaway: odd outer loop (range(..., 2)), reverse inner loop with if j > i, persistent k += 1, then print() after each row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Safe input (Example 3) | O(n²) | O(1) |
The centered continuous number pyramid is a compact lesson in spacing and counters: print leading spaces when j > i, then k += 1 for each number slot. Master the fixed-max_n version, then try user input and safe input() validation.
Practice the three examples above, then continue to Program 25 for the bidirectional number triangle.
Never reset k inside the outer loop unless you want per-row numbering — validate max_n when reading from the console.
for i in range(1, max_n + 1, 2) in the outer loopk = 1 before the outer loopj > i, else print k and k += 1int(input()) in try/except ValueError before using max_nprint() inside the inner loopk inside the outer loop (unless intentional)max_n without adjustmentmax_n = 1 edge casePrint the pattern the beginner-friendly way.
Spaces + k += 1
DefinitionOdd widths
CodeCentering
CodeNever reset
ShapeO(n²) time
AnalysisThis centered pyramid prints numbers continuously using a counter k. An if inside a reverse loop prints leading spaces when j > i, then prints k and does k += 1 once the column reaches the row boundary.
Move on to the bidirectional number triangle in the Python number-pattern series.
12 people found this page helpful