max python

Just another WordPress site

Display a Sandglass Star Pattern using python

In this article we display a sandglass pattern using python

Code

rows = int(input("Enter amount of Rows = "))

print("====Pattern====")

for i in range(0, rows):
    for j in range(0, i):
        print(end = ' ')
    for k in range(i, rows):
        print('*', end = ' ')                
    print()

for i in range(rows - 1, -1, -1):
    for j in range(0, i):
        print(end = ' ')
    for k in range(i, rows):
        print('*', end = ' ')      
    print()

When you run this you would see something like this

Alternative

def drawSandglass(rows, ch):
    for i in range(0, rows):
        for j in range(0, i):
            print(end = ' ')
        for k in range(i, rows):
            print('%c' %ch, end = ' ')               
        print()

    for i in range(rows - 1, -1, -1):
        for j in range(0, i):
            print(end = ' ')
        for k in range(i, rows):
            print('%c' %ch, end = ' ')     
        print()
    
rows = int(input("Enter amount of Rows = "))

char = input("Symbol to use = " )

print("====Pattern====")
drawSandglass(rows, char)

 

Link

sandglasspattern1

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views :