Skip to content

Instantly share code, notes, and snippets.

@1st
Last active February 28, 2025 10:39
Show Gist options
  • Save 1st/a3ef5d60fb2b9e1db754f77686e56840 to your computer and use it in GitHub Desktop.
Save 1st/a3ef5d60fb2b9e1db754f77686e56840 to your computer and use it in GitHub Desktop.
Print a Christmas tree in Python
# Task: Given a number of tree levels. Print a Christmas tree with the given levels, filled by star symbols (*).
# Example:
#
# print_a_tree(20)
#
# *
# ***
# *****
# *******
# *********
# ***********
# *************
# ***************
# *****************
# *******************
# *********************
# ***********************
# *************************
# ***************************
# *****************************
# *******************************
# *********************************
# ***********************************
# *************************************
# ***************************************
# Solution:
def print_tree_level(level: int, levels: int):
spaces = levels - level
stars = 2 * (level - 1) + 1
print(' ' * spaces, '*' * stars)
def print_a_tree(levels: int):
for level in range(1, levels + 1):
print_tree_level(level, levels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment