Home ยป Print Christmas Tree Star Pattern using python

Print Christmas Tree Star Pattern using python

In this article we will draw a simple christmas tree using python

Code

width = int(input("Enter Christmas Tree Width   = "))
height = int(input("Enter Christmas Tree Height = "))

space = width * height
n = 1

print("====The Christmas Tree Star Pattern====")

for x in range(1, height + 1):
    for i in range(n, width + 1):
        for j in range(space, i - 1, -1):
            print(end = ' ')
        for k in range(1, i + 1):
            print('*', end = ' ')
        print()
    n = n + 2
    width = width + 2

for i in range(1, height):
    for j in range(space - 3, -1, -1):
        print(end = ' ')
    for k in range(1, height):
        print('*', end = ' ')
    print()

Output

This is an example with 3 for width and height

Alternative

 

# Generating Triangle
def triangleShape(n):
    for i in range(n):
        for j in range(n-i):
            print(' ', end=' ')
        for k in range(2*i+1):
            print('*',end=' ')
        print()

# Generate stump
def poleShape(n):
    for i in range(n):
        for j in range(n-1):
            print(' ', end=' ')
        print('* * *')

# Input and Function Call
row = int(input('Enter number of rows: '))

triangleShape(row)
triangleShape(row)
poleShape(row)

 

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More