Python Number Pyramid Pattern (Centered Continuous)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A centered continuous number pyramid prints odd-width rows with leading spaces, while a shared counter k keeps climbing across every row.
Remember
Rule: odd widths i = 1, 3, 5, …
space when j > i, else print k += 1
1
2 3 4
5 6 7 8 9 ← max_n = 5
Combines Program 20’s running counter with centering spaces — a natural step after the mirror in Program 23.
Approach
How to Solve It
Walk odd widths with range(..., 2); scan j from max_n down — space if j > i, else print k and bump.
Method
Idea
Best for
Reverse + if
Spaces then k += 1 in one loop
Learning, interviews
input() max
Same loops; force odd max if needed
Interactive practice
Safe re-prompt
while until valid positive input
Robust labs
Pseudocode
Pseudocode
k = 1
for i from 1 to max_n step 2:
for j from max_n down to 1:
if j > i:
print space (same line)
else:
print k (same line, spaced)
k = k + 1
print newline
Cheat sheet
Goal
Pattern
Set max
max_n = 5
Init counter
k = 1 before both loops
Outer loop
for i in range(1, max_n + 1, 2):
Inner reverse
for j in range(max_n, 0, -1):
Space vs number
if j > i: print(" ", end="") else: print(k, end=" "); k += 1
End row
print()
Force odd max
if max_n % 2 == 0: max_n -= 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(" ", end="") / print(k, end=" ")
Stays on the same line
Spaces and numbers
print()
Ends the current line
After the inner loop
Print spaces/numbers with end=, then break once with print(). Putting print() inside the inner loop prints one character group per line.
Try it
Live Preview
Change the max odd width and the pyramid updates instantly. Even values are adjusted down by 1.
Whole numbers from 1 to 9. Even inputs become the next lower odd value.
Live resultmax = 5 · numbers = 9
1
2 3 4
5 6 7 8 9
Trace
Worked Walkthrough — max_n = 3
Trace each odd i, how many leading spaces, and which values of k are printed.
i
Spaces (j > i)
Numbers
Printed row
1
2 (j = 3, 2)
1
1
3
none
2 3 4
2 3 4
After two rows, k is 5 — ready for a wider pyramid if max_n grows.
Code
Python Programs
Three complete programs: fixed max_n = 5, input() max, and a safe re-prompt loop. Use View Output for sample results.
Example 1 — Fixed max_n = 5
Hard-coded odd width — reverse inner loop handles spaces vs k += 1.
Python
max_n = 5
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()
Output
1
2 3 4
5 6 7 8 9
How It Works
1. Odd widths.i runs 1, 3, 5 — those are how many numbers each row prints.
2. Reverse scan.j counts down from max_n; when j > i, print a leading space.
3. Shared counter. Otherwise print k with a trailing space and k += 1; k survives into the next row.
Example 2 — input() Max
Read the max width at runtime; if even, subtract 1 so row sizes stay odd.
Python
max_n = int(input("Enter the maximum odd width: "))
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()
Output (when user enters 5)
Enter the maximum odd width: 5
1
2 3 4
5 6 7 8 9
How It Works
1. Read max. Convert the prompt answer with int().
2. Oddify if needed. If even, max_n -= 1 keeps odd row widths.
3. Same loops. Reverse j scan and shared k match Example 1.
Example 3 — Safe Re-prompt Loop
Keep asking until the user enters a positive whole number, then draw the pyramid.
Python
max_n = 0
while max_n < 1:
raw = input("Enter the maximum odd width: ").strip()
try:
max_n = int(raw)
except ValueError:
max_n = 0
if max_n < 1:
print("Please enter a positive whole number.")
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()
Output (when user enters 3)
Enter the maximum odd width: 3
1
2 3 4
How It Works
1. Re-prompt. The while loop keeps max_n at 0 until input is a positive integer.
2. Then oddify. Even values still drop by 1 before drawing.
3. Same pyramid. The nested loops are unchanged once max_n is ready.
Edge Cases & Pitfalls
Check these before calling the solution done.
k inside
Counter resets
If k = 1 is inside the outer loop, every row restarts at 1. Keep k outside both loops.
no step
Forgot step 2
Using range(1, max_n + 1) prints every width 1, 2, 3, … — not only odd pyramid rows. Keep step 2.
print() inside
Broken rows
If print() is inside the inner loop, spaces and numbers split across lines. Call it only after the reverse scan.
even max
Even maximum
With max_n = 6, call max_n -= 1 first so the last width is 5 (odd).
max_n = 1
Single number
Output is just 1 on one line — no leading spaces.
input()
Catch ValueError
Letters crash bare int(input()) — wrap in try/except (see Example 3).
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(1)
About n/2 rows, each scanning n columns → O(n²).
Remember
Key Takeaways
Rule: odd width i; space when j > i; else print k and bump.
Init once: set k = 1 before the loops so numbers continue across rows.
end= vs print(): spaces/numbers stay on the line; print() advances after each row.
Complexity:O(n²) for max width n.
One line: grow odd widths, pad with spaces on the left, and never reset the counter between rows.
Frequently Asked Questions
The program prints a space when j > i in the reverse inner loop, which shifts the numbers right and centers each row.
Because the counter k is assigned once before the outer loop and increments with k += 1 every time a number is printed — it never resets.
Row widths are odd (1, 3, 5, …) so each row adds two more numbers than the previous row.
print(" ", end="") and print(k, end=" ") stay on the same line. print() ends the current row after the inner loop finishes.
The reverse inner loop prints leading spaces first (when j > i) and numbers afterward — a common centering trick.
Subtract 1 to force an odd width (see Example 2), or validate and prompt again.
O(n²) for max width n because each row iterates across n columns.
Use try/except ValueError around int(input()), and optionally a while loop that re-prompts on failure (see Example 3).
Only one row prints — a single 1 (with trailing space).
🤔
Did you know?
This 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.