This Python code generates a left arrow pattern of asterisks based on user input. The pattern consists of two parts: an ascending pattern and a descending pattern.
# Get the number of rows from the user
num_rows = int(input("Enter the number of rows: "))
# Display the left arrow pattern
for i in range(1, num_rows + 1):
print(" " * (num_rows - i), end="")
print("*" * i)
for i in range(num_rows - 1, 0, -1):
print(" " * (num_rows - i), end="")
print("*" * i)
*
**
***
****
*****
****
***
**
*
num_rows = int(input("Enter the number of rows: "))
num_rows
.for i in range(1, num_rows + 1):
print(" " * (num_rows - i), end="")
print("*" * i)
for i in range(1, num_rows + 1)
num_rows
inclusive. Each iteration represents a row in the pattern.print(" " * (num_rows - i), end="")
(num_rows - i)
. The number of spaces decreases as i
increases, creating the left-aligned effect.end=""
parameter prevents the print function from moving to a new line, allowing the asterisks to be printed on the same line.print("*" * i)
i
asterisks. The number of asterisks increases with each row, creating the ascending part of the pattern.If num_rows
is 5
, the output for this part will be:
*
**
***
****
*****
for i in range(num_rows - 1, 0, -1):
print(" " * (num_rows - i), end="")
print("*" * i)
for i in range(num_rows - 1, 0, -1)
num_rows - 1
down to 1 inclusive. Each iteration represents a row in the descending pattern.print(" " * (num_rows - i), end="")
(num_rows - i)
. The number of spaces increases as i
decreases, maintaining the left-aligned effect.end=""
parameter ensures the asterisks are printed on the same line.print("*" * i)
i
asterisks. The number of asterisks decreases with each row, creating the descending part of the pattern.If num_rows
is 5
, the output for this part will be:
****
***
**
*
When the user inputs 5
for num_rows
, the combined output will be:
*
**
***
****
*****
****
***
**
*
This pattern creates a left arrow shape with the top half having an increasing number of asterisks and the bottom half having a decreasing number of asterisks. The spaces ensure the pattern is aligned to the left.