Print Inverted Right Triangle
Print a shrinking right triangle of stars in Python. Students trace a descending row width and keep the newline outside the inner character-printing loop.
1# Take n2n = int(input("Enter number of rows: "))34# Print inverted triangle5for i in range(n, 0, -1):6 for j in range(1, i + 1):7 print("*", end="")8 print()
Output
Enter number of rows: 5 ***** **** *** ** *
In this one
- Shrink row widths
- Use a descending range
- Keep inner counting independent
How it works
- 1
Visit widths from n down to one.
- 2
Print the current number of stars.
- 3
End each completed row with print().
Edge cases
Is there a zero-star row?
Is there a zero-star row?
No. The outer stop zero is excluded.
What if n = 0?
What if n = 0?
The outer range is empty.
What's going on
The outer range begins at n and decreases to one. Its current i is the number of stars required on that row.
The inner loop still counts upward, but only to the current width. For n equal to three, rows contain three, two, then one star. The two loop directions serve different jobs: row size shrinks while each row is printed left to right.
Is there a zero-star row? No. The outer stop zero is excluded.
What if n = 0? The outer range is empty.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What changes between rows?
Pick an answer to check it.
Question 2
Is there a zero-star row?
Pick an answer to check it.
Questions people ask
Is there a zero-star row?
No. The outer stop zero is excluded.
What if n = 0?
The outer range is empty.
What changes between rows?
The number of printed stars decreases.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum