W3D1 Lecture practice assignment

Code-1

line = ‘*’

n = 5

while len(line) <= 6 :

print(" "*n + line)

line = line + "*"

n -= 1   

while n < 5:

print(" "*(n+2) + line[n+2:-1])

n += 1

Desired output:-
*
**
***
****
*****
******
*****
****
***
**
*

Code-2

line = ‘*’

n = 5

while len(line) <= 11 :

   print(" "*n + line + " "*n)

   line = line + "**"

   n -= 1   

while n < 5:

   print(" "*(n+2) + line[n+2:-(n+4)] + " "*(n+2))

   n += 1

desired output:-
*
***
*****
*******
*********
***********
*********
*******
*****
***
*

above codes are giving desired output, is there any other solution for this?

You can use this code to get your desired output

# upper part

row = 5

for i in range(0,row):

  for j in range(i+1):

    print("* ",end="")

  print("\r")

#lower part

for i in range(row, 0, -1):    

    for j in range(0, i - 1):  

        print("*", end=' ')  

    print("\r")