So question about printing *(star) in specific shape using nested for

n = int(input(“How many rows do you want to print numbers?:”))

        for i in range(1,n*2):
            for j in range(1,n*2):
                if (i==j):
                    print('*', end='')
                elif i+j==2*n:
                    print('**', end='')
                    
                else:
                    print(' ', end='')
            print()  

I want to get output as

how am i able to fix this code

Thank for supporting

Well, here is what I could come up with. I added some error handling to make sure the user isn’t inputting anything invalid.

while True:
    try:
        n = int(input('How many?\n'))
    except ValueError:
        print('That is not a valid number')
    else:
        break

if n <= 1:
    print('*')
else:
    print('* *')
    for _ in range(n - 2):
        print('**')
    print('* *')
1 Like