How to Display Christmas Tree Star Pattern in Python

In this tutorial, we will learn how to program "How to Display a Christmas Tree Star Pattern in Python." We will explore how to create and display a Christmas tree using stars (*). The objective is to generate a visually accurate representation of a Christmas tree using nested loops. I will provide a sample program to demonstrate the coding process step by step.

This topic is very easy to understand. Just follow the instructions I provide, and you'll be able to do it yourself with ease. The program I’ll show you demonstrates the proper way to display a Christmas tree star pattern in Python. I’ll also provide a simple and efficient method to achieve this. So, let's start coding!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. ret = False
  2.  
  3. while True:
  4.         print("\n================= Display Christmas Tree Star Pattern =================\n\n")
  5.  
  6.  
  7.         num = 4
  8.         for i in range(1, num+1):
  9.                 print(" "*(2*num-i+3), end="")
  10.                 for j in range(1, i+1):
  11.                         print("*", end=" ")
  12.                 print()
  13.  
  14.         for i in range(1, num+3):
  15.                 print(" "*(2*num-i+1), end="")
  16.                 for j in range(-1, i+1):
  17.                         print("*", end=" ")
  18.                 print()
  19.  
  20.         for i in range(1, num+5):
  21.                 print(" "*(2*num-i), end="")
  22.                 for j in range(-2, i+1):
  23.                         print("*", end=" ")
  24.                 print()
  25.  
  26.         for i in range(1, num+3):
  27.                 print(" "*((2*num)), end="")
  28.                 print("* "*3)
  29.  
  30.                
  31.         opt = input("\nDo you want to try again?(yes/no): ")
  32.  
  33.         if opt.lower() == 'yes':
  34.                 ret=False
  35.         elif opt.lower() == 'no':
  36.                 ret=True
  37.                 print("Exiting program....")
  38.  
  39.         else:
  40.                 print("Please enter yes/no:")
  41.                 break
  42.  
  43.         if ret == False:
  44.                 continue

This script generates a star pattern resembling a Christmas tree. It uses nested loops to print different sections of the tree, including the triangular parts and the trunk. The program first displays a small triangular section, followed by progressively larger sections, then concludes with a trunk made of stars.

The script keeps running in a loop, allowing the user to repeat or exit the program based on their input (yes or no). Each loop iteration redraws the star pattern.

Output:

There you have it we successfully created How to Display Christmas Tree Star Pattern in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment