Python Print an Inverted Triangle Star Pattern
We will use Python to write a simple program to print an inverted triangle star pattern. This pattern will consist of multiple rows of asterisks, with the number of asterisks decreasing row by row.
Example
def print_inverted_triangle(n):
for i in range(n, 0, -1):
print('*' * i)
# Call the function to print a 5-row inverted triangle
print_inverted_triangle(5)
for i in range(n, 0, -1):
print('*' * i)
# Call the function to print a 5-row inverted triangle
print_inverted_triangle(5)
Code analysis:
def print_inverted_triangle(n):Defined a function namedprint_inverted_trianglefunction, which accepts a parametern, representing the number of rows of the inverted triangle.for i in range(n, 0, -1):Useforloop fromndescending to 1, each iterationithe value will decrease by 1.print('*' * i)In each iteration, printiasterisks.'*' * iindicates repeating the asteriskitimes.print_inverted_triangle(5)Call the function to print a 5-row inverted triangle.
Output:
***** **** *** ** *Other Extensions
Python3 Example