📅  最后修改于: 2023-12-03 15:00:03.733000             🧑  作者: Mango
To create a program that displays an isosceles triangle of stars with N rows (where N is provided by the user via keyboard input), you can use the following code snippet:
def display_isosceles_triangle(n):
for i in range(1, n+1):
spaces = ' ' * (n-i)
stars = '*' * (2*i - 1)
print(spaces + stars)
# Taking input from the user
num_rows = int(input("Enter the number of rows for the isosceles triangle: "))
# Displaying the isosceles triangle
display_isosceles_triangle(num_rows)
In the above code:
display_isosceles_triangle
that takes the number of rows (n
) as a parameter.n
(inclusive) using a for
loop, representing each row of the triangle.spaces
) is equal to (n-i)
to create the triangular shape.stars
) is equal to (2*i - 1)
to form the isosceles pattern.spaces
and stars
for each row, which forms the triangle.To use this program, the user needs to input the desired number of rows for the isosceles triangle. The program will then display the triangle by calling the display_isosceles_triangle
function.
Please ensure to properly format the code snippet within the markdown syntax to preserve the indentation and readability.