Star Shape Python

Viewed 27

I'm trying to recreate this shape (image below) using Python but I have some difficulties with the spaces.

This is the code that I have so far.

def shape(n):
    for i in range(0,n):
        for j in range(0,i+1):
            print("*",end="")
        print("")
    for i in range(n,0,-1):
        for j in range(0,i-1):
            print("","*",end="")
        print("")
        
shape(10)

I would appreciate it if you could help me. star shape

2 Answers
def star(width = 10):
    left_start = 0
    right_start = 0
    size = width
    while right_start != size:
        right_start += 1
        print("*" * (right_start - left_start))
    
    while left_start != right_start:
        left_start += 1
        print(" " * left_start + "*" * (right_start - left_start))

Hope this helps!

def shape(n):
    for i in range(0,n):
        for j in range(0,i+1):
            print("*",end="")
        print("")
    for i in range(n,0,-1):
            print(" "*(n-i)+"*"*i)
        
    
shape(10)

Remove the last for loop and do the following change. It will give the desired output.

Related