📜  Créer un program permettant d'afficher un triangle isocèle formé d'étoiles de N lignes (N étantfourni au clavier):(1)

📅  最后修改于: 2023-12-03 15:00:03.733000             🧑  作者: Mango

Creating a Program to Display an Isosceles Triangle of Stars

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:

  1. We define a function display_isosceles_triangle that takes the number of rows (n) as a parameter.
  2. We iterate from 1 to n (inclusive) using a for loop, representing each row of the triangle.
  3. Inside the loop, we calculate the number of spaces and stars to be displayed in each row:
    • The number of spaces (spaces) is equal to (n-i) to create the triangular shape.
    • The number of stars (stars) is equal to (2*i - 1) to form the isosceles pattern.
  4. We print the combination of 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.