An inverted triangle is the star triangle upside down: the first row is the longest, and each row has one star less.
The program
python
rows = int(input("Number of rows: ")) for i in range(rows, 0, -1): print("*" * i)
Typed in this example: 5. Press Run to type your own.
output
Number of rows: 5 ***** **** *** ** *
How it works
range(rows, 0, -1)counts down: 5, 4, 3, 2, 1. The third number,-1, is the step. The end value 0 is not included."*" * irepeats the staritimes, so the rows get shorter.
Other inverted shapes
The same countdown loop prints three more shapes. Each row puts its spaces in front, so no line ends with a space:
python
rows = 5 print("Right-aligned:") for i in range(rows, 0, -1): print(" " * (rows - i) + "*" * i) print("Inverted pyramid:") for i in range(rows, 0, -1): print(" " * (rows - i) + "*" * (2 * i - 1)) print("Numbers:") for i in range(rows, 0, -1): print(*range(1, i + 1))
output
Right-aligned:
*****
****
***
**
*
Inverted pyramid:
*********
*******
*****
***
*
Numbers:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1- In the right-aligned triangle, each row has one more space and one star less, so the length of every line is 5.
- The inverted pyramid has
2 * i - 1stars per row (9, 7, 5, 3, 1). Put it under a normal pyramid and you get a diamond. print(*range(1, i + 1))prints the numbers 1 toiwith spaces between them.