'Using for loops to create a christmas tree

I am trying to create a program where you enter a number and the program creates a "christmastree" arrangement of +'s. For example if I enter the number 5 the program should print:

    +
   +++
  +++++
 +++++++
+++++++++

What I have so far is:

def holidaybush(n):
    z=n-1
    x=1
    for i in range(0,n):
        for i in range(0,z):
            print('',end='')
        for i in range(0,x):
            print('+',end='')
        for i in range(0,z):
            print('',end='')
        x=x*2
        x=x-1
        z=z-1
        print()
holidaybush(5)

It does not work quite the way I expect, even though I go through the logic and it seems to work in my head. Any help? I just learned for loops today so I may not know everything about them.



Solution 1:[1]

how about this:

def holidaybush(n):
    for i in range(n):
        print ' ' * (n - (i + 1)),'+' * (2*i+1)

holidaybush(5)

Solution 2:[2]

You could use string.center(), just for adding another solution, this made the code more compact:

def holidaybush(n):
    for i in range(n):
        print(("+" * (i * 2 + 1)).center(n * 2 - 1))

holidaybush(5)

Solution 3:[3]

This is the very simplest code to draw Christmas tree:

for i in range(1,20,2):
    print(('*'*i).center(20))

for leg in range(3):
    print(('||').center(20))

print(('\====/').center(20))

Solution 4:[4]

My one line solution.

def holiday_bush(x):
    print("\n".join([f"{'+'*(2* n + 1):^{2*x+1}}" for n in range(x)]))

Result:

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

If you want also a trunk on the base:

def holiday_bush(x):
    print("\n".join([f"{'+'*(2* n + 1):^{2*x+1}}" for n in (*range(x), 0, 0)]))

Result:

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

Solution 5:[5]

If you change the 'x+=2' to 'x+=1' you get the right shape, but not as many '+', but it won't be as wide mod:

def holidaybush(n):
z = n 
x =1

for i in range(n):
    print(' ' * z + '+' * x + ' ' * z)
    x+=1
    z-=1


holidaybush(5)

Solution 6:[6]

We can also do it without any numbers, just for fun

n = int(input("how big?\t"))
for i in range(n):
    for j in range(n-i):
        print(' ', end='')
    print('\b', end='') #cheating
    for k in range(i+n-j):
        print('*', end='')
    print('')

Solution 7:[7]

number = -1
Range = int(input("how many layers do you want the tree?"))
for x in range(0,Range):
    number = number + 2
    print(" " * (Range - x), "+" * number)

This worked for me.

Solution 8:[8]

`

a=7
n=7
for i in range(7):
  print(" "*a,end="")
  print("*"*(2*i+1))
  a-=1
for b in range (n//2):
  print("     *****")

` try this simple code to print the tree using for loop

Solution 9:[9]

while True:
    size = int(input("Enter the number of rows for the tree:"))
    last_row = (2 * size) - 1
    for i in range(1, size + 1):
        print(((" ") * (size - i)) + (("*") * ((2 * i) - 1)))

I came up with this code for the tree program.

Solution 10:[10]

height = eval(input("Enter height of tree: "))
for row in range(height):
    for count in range(height - row):
        print(end=" ")
    for count in range(2*row + 1):
        print(end="*")
    print()