π Python Pattern Challenge — Day 20
Pattern printing is a great way to strengthen your Python loops, spacing, repetition, and logical thinking. For Day 20, let's create a simple but interesting Star Hourglass Pattern ⭐.
This pattern starts with multiple stars, gradually narrows down to a single star, stays narrow for a few rows, and then expands again.
π― Today's Challenge
Write a Python program to print:
Best and cleanest code will be rewarded! π
Solution 1 — Using Two for Loops
n = 5for i in range(n, 0, -1): print(" " * (n - i) + "* " * i) for i in range(1, n + 1): print(" " * (n - i) + "* " * i)
How it works
The first loop creates the decreasing section:
* * * * * * * * * * * * * * *
The second loop creates the increasing section:
** * * * * * * * * * * * * *
Together, they form the hourglass-like pattern.
Solution 2 — Matching the Exact Challenge Pattern
The image contains three single-star rows in the center.
n = 5 for i in range(n, 0, -1): print(" " * (n - i) + "* " * i) for _ in range(2): print(" " * (n - 1) + "* ") for i in range(2, n + 1): print(" " * (n - i) + "* " * i)
Output
This version matches the Day 20 challenge pattern more closely.
Solution 3 — Using a Single Loop
n = 5 pattern = list(range(n, 0, -1)) + [1, 1] + list(range(2, n + 1)) for i in pattern: print(" " * (n - i) + "* " * i)
The pattern sequence is:
5, 4, 3, 2, 1, 1, 1, 2, 3, 4, 5Each value determines how many stars appear on that row.
⚡ Short & Clean Code
for i in [5, 4, 3, 2, 1, 1, 1, 2, 3, 4, 5]: print(" " * (5-i) + "* " * i)
π₯ Just one loop creates the complete pattern.
π Challenge Yourself
Can you modify this pattern:
- Take n from the user using input()?
- Create it using a while loop?
- Increase the number of center rows?
- Replace * with # or another symbol?
- Create a hollow version?
- Make the pattern wider or taller?
Drop your solution below! π
20 Days. 20 Patterns. Stronger Python Logic. ππ₯
Learn • Practice • Grow with CLCODING π

0 Comments:
Post a Comment