Using for loops to create a christmas tree
Question:
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.
Answers:
OK, you have two problems. First, when you go to do your indentation, you write:
print('',end='')
In python (and other languages), ''
is an empty string. You should use ' '
.
Second, your x
incrementing logic seems to be wrong. Simply adding 2
each loop works fine, making your program:
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
z=z-1
print()
holidaybush(5)
Your code can be made more compact by:
- Using infix operators, replacing
x=x+2
with x+=2
range
automatically starts at zero, so range(0,z)
can be replaced with range(z)
- Using string multiplication, replacing your inner
for
loops with ' ' * z
Applying these results in:
def holidaybush(n):
z = n - 1
x = 1
for i in range(n):
print(' ' * z + '+' * x + ' ' * z)
x+=2
z-=1
holidaybush(5)
But you might want to stick with the verbose version.
how about this:
def holidaybush(n):
for i in range(n):
print ' ' * (n - (i + 1)),'+' * (2*i+1)
holidaybush(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)
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)
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()
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('')
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.
`
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
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))
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.
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:
*
***
*****
*******
*********
*
*
widzialem lepszea choinhi
Piotr Gryfinski
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.
OK, you have two problems. First, when you go to do your indentation, you write:
print('',end='')
In python (and other languages), ''
is an empty string. You should use ' '
.
Second, your x
incrementing logic seems to be wrong. Simply adding 2
each loop works fine, making your program:
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
z=z-1
print()
holidaybush(5)
Your code can be made more compact by:
- Using infix operators, replacing
x=x+2
withx+=2
range
automatically starts at zero, sorange(0,z)
can be replaced withrange(z)
- Using string multiplication, replacing your inner
for
loops with' ' * z
Applying these results in:
def holidaybush(n):
z = n - 1
x = 1
for i in range(n):
print(' ' * z + '+' * x + ' ' * z)
x+=2
z-=1
holidaybush(5)
But you might want to stick with the verbose version.
how about this:
def holidaybush(n):
for i in range(n):
print ' ' * (n - (i + 1)),'+' * (2*i+1)
holidaybush(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)
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)
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()
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('')
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.
`
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
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))
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.
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:
*
***
*****
*******
*********
*
*
widzialem lepszea choinhi
Piotr Gryfinski