Skip to main content

Day-1 Create an equilateral triangle pattern (PYRAMID) with stars using python

                                                                     FIRST


With no space in between the stars:

def space(n,i):
    c=n-i
    if c>0:
        print(" "*c, end="")

def star(i):
    if i>0:
        print("*"*(2*i-1), end="")

def start():
    n=int(input("Enter the rows: "))
    b=n
    i=1
    while n>0: #3
         #1
        space(b,i)# 1 2 3 
        star(i)
        space(b,i)
        print()
        i=i+1
        n=n-1

start()

Let's go through the code step by step:

  1. The space() function takes two parameters, n and i, which represent the total number of spaces (n) and the current row number (i).

  2. Inside the space() function, the variable c is calculated as the difference between n and i, representing the number of spaces needed in the current row.

  3. If c is greater than 0, a space character is printed c number of times using the " "*c expression. The end="" parameter is used to avoid printing a newline character.

  4. The star() function takes the i parameter, representing the current row number.

  5. If i is greater than 0, the expression "*" is multiplied by (2*i-1), which gives the number of stars in the current row. This ensures that the number of stars follows the pattern of an equilateral triangle.

  6. Inside the start() function, the number of rows n is obtained from user input using input() and int() functions.

  7. The variable b is assigned the value of n to store the initial number of spaces needed.

  8. The variable i is initialized to 1, representing the current row number.

  9. The while loop executes as long as n is greater than 0, indicating there are more rows to print.

  10. Inside the loop, the space() function is called with b and i as arguments to print the required number of spaces before the stars in each row.

  11. The star() function is called with i as an argument to print the stars in each row.

  12. After printing the stars, the space() function is called again with b and i as arguments to print the spaces after the stars.

  13. A newline character is printed using print() to move to the next row.

  14. The variables i and n are incremented and decremented, respectively, to move to the next row.

  15. The while loop continues until all rows are printed.


SECOND


With  space in between the stars:

def space(n, i):
    c = n - i
    if c > 0:
        print(" " * c, end=" ")

def star(i):
    if i > 0:
        print("* " * i, end="")

def start():
    n = int(input("Enter the rows: "))
    b = n
    i = 1
    while n > 0:
        space(b, i)
        star(i)
        space(b, i)
        print()
        i += 1
        n -= 1

start()

The changes made are:

  1. In the space function, an extra space character is added after printing the spaces to achieve the desired spacing between the spaces and the stars.
  2. In the star function, the "*" is followed by a space character to achieve the desired spacing between the stars.

Now let's try something different:



def space(n,i):
    c=n-i
    if(c>0):
        print(" "*c, end="")
       
def star(i):
    if(i>0):
        print("* "*(2*i-1), end="")

def start():
    n=int(input("Enter the rows: "))
    b=n
    i=1
    while n>0:
        space(b,i)
        star(i)
        space(b,i)
        print()
        i=i+1
        n=n-1

start()



Comments