Tushar Sharma
Tushar Sharma

Reputation: 19

the pattern i m getting in my output have the extra star at the end which is not required

The pattern I'm getting in my output have the extra star at the end which is not required, this is what I tried my self:

n=int(input("enter no"))
for i in range(1,n+1):
    for j in range(i):
        print("*",end="")
    for k in range(1,(2*n)-2*i):
        print(" ",end="")
    for l in range(i):
        print("*", end="")
    print("")

I don't want extra star at the end.

Upvotes: 1

Views: 31

Answers (1)

Djaouad
Djaouad

Reputation: 22776

You need to stop your loop at n - 1, and after the loop, print 2*n - 1 "*":

n=int(input("enter no"))

for i in range(1, n):
    for j in range(i):
        print("*", end="")
    for k in range(1, 2*n - 2*i):
        print(" ", end="")
    for l in range(i):
        print("*", end="")
    print()

print("*" * (2*n - 1))

Output (for n = 5):

*       *
**     **
***   ***
**** ****
*********

Output (for n = 6):

*         *
**       **
***     ***
****   ****
***** *****
***********

Upvotes: 1

Related Questions