[How to create] – Dynamic Stars Triangle With Python

There are many ways to learn the algorithms in science programming. One of them by making a simple program that contains logic. On this occasion, we try to make a simple python based program to train your logical thinking. Here I use the Python 3.6 interpreter that runs both on Windows. On this case I tried to use while loops to make this simple program.

1.For the first, we make a triangle with the shape of the bottom left alignment. Then create a file name triangle_01.py and save them in your directory.

string = ""
bar = 1

x = int(input("Please enter a number :"))
print("\n")
# Looping rows
while bar <= x: 
     
     # Looping columns 
     kol = bar 
     while kol > 0:
          string = string + " * "
          kol = kol - 1
		
     string = string + "\n"
     bar = bar + 1
print (string)

Now, look at the results by using the command : python triangle_01.py

triangle_stars_01

2. Now, we make a triangle with the shape of the top left alignment. Then create a file name triangle_02.py and save them in your directory.

string = ""

bar = int(input("Please enter a number :"))
print("\n")
# Looping rows
while bar >= 0:
     
     # Looping columns
     kol = bar
     while kol > 0:
          string = string + " * "
          kol = kol - 1
		
     string = string + "\n"
     bar = bar - 1
print (string)

And then, look at the results by using the command : python triangle_02.py

triangle_stars_02

3. Now, we make a triangle with the shape of the bottom right alignment. Then create a file name triangle_03.py and save them in your directory.

string = ""

x = int(input("Please enter a number :"))
bar = x
# Looping rows
while bar >= 0:
     
     # Looping space empty
     kol = bar
     while kol > 0:
          string = string + "   "
          kol = kol - 1	

     # Looping columns stars
     kanan = 1
     while kanan < (x - (bar-1)):
          string = string + " * "
          kanan = kanan + 1			
     
     string = string + "\n"
     bar = bar - 1	
print (string)

And then, look at the results by using the command : python triangle_03.py

triangle_stars_03

4. Now, we make a triangle with the shape of the top right alignment. Then create a file name triangle_04.py and save them in your directory.

string = ""
bar = 1

x = int(input("Please enter a number :"))
print("\n")
# Looping rows
while bar <= x: 

     # Looping empty spaces 
     kol = bar 
     while kol > 1:
          string = string + "   "
          kol = kol - 1
	
     # Looping columns stars
     kanan = 0
     while kanan <= (x - bar):
          string = string + " * "
          kanan = kanan + 1	
		
     string = string + "\n"
     bar = bar + 1
print (string)

And then, look at the results by using the command : python triangle_04.py

triangle_stars_04

5. Now, we make a triangle with the shape of the bottom center alignment. Then create a file name triangle_05.py and save them in your directory.

string = ""

x = int(input("Please enter a number :"))
bar = x
# Looping rows
while bar >= 0:
	
     # Looping empty spaces
     kol = bar
     while kol > 0:
          string = string + "   "
          kol = kol - 1
	
     # Looping columns stars left
     kiri = 1
     while kiri < (x - (bar-1)):
          string = string + " * "
          kiri = kiri + 1		

     # Looping columns stars right
     kanan = 1
     while kanan < kiri -1:
          string = string + " * "
          kanan = kanan + 1	

     string = string + "\n\n"
     bar = bar - 1
print (string)

And then, look at the results by using the command : python triangle_05.py

triangle_stars_05

6. Now, we make a triangle with the shape of the top center alignment. Then create a file name triangle_06.py and save them in your directory.

string = ""
bar = 1

x = int(input("Please enter a number :"))
print ("\n")
# Looping rows
while bar <= x: 
     
     # Looping empty spaces 
     kol = bar 
     while kol > 1:
          string = string + "   "
          kol = kol - 1
	
     # Looping columns stars left	
     kiri = 0
     while kiri <= (x - bar): 
          string = string + " * " 
          kiri = kiri + 1 

     # Looping columns stars right 
     kanan = kiri 
     while kanan > 1:
          string = string + " * "
          kanan = kanan - 1

     string = string + "\n\n"
     bar = bar + 1
print (string)

And then, look at the results by using the command : python triangle_06.py

triangle_stars_06

7. Now, we make a triangle with the shape of the center alignment (rhombus). Then create a file name triangle_07.py and save them in your directory.

string = ""

x = int(input("Please enter a number :"))
bar = x
# Looping rows
while bar >= 0:

     # Looping empty spaces
     kol = bar
     while kol > 0:
          string = string + "   "
          kol = kol - 1

     # Looping columns stars left		
     kiri = 1
     while kiri < (x - (bar-1)):
          string = string + " * "
          kiri = kiri + 1
		
     # Looping columns stars right
     kanan = 1
     while kanan < kiri -1:
          string = string + " * "
          kanan = kanan + 1	

     string = string + "\n\n"
     bar = bar - 1

bar = 1	
# Looping rows
while bar <= x: 
     # Looping empty spaces 
     kol = bar+1 
     while kol > 1:
          string = string + "   "
          kol = kol - 1

     # Looping columns stars left	
     kiri = 0
     while kiri < (x - bar): 
          string = string + " * " 
          kiri = kiri + 1 

     # Looping columns stars right 
     kanan = kiri 
     while kanan > 1:
          string = string + " * "
          kanan = kanan - 1

     string = string + "\n\n"
     bar = bar + 1
print (string)

And then, look at the results by using the command : python triangle_07.py

triangle_stars_07

8. Now, we make a triangle with the shape of the center middle alignment. Then create a file name triangle_08.py and save them in your directory.

string = ""
bar = 1

x = int(input("Please enter a number :"))
print ("\n")
# Looping rows
while bar < x: 
     # Looping empty spaces 
     kol = bar 
     while kol > 1:
          string = string + "   "
          kol = kol - 1

     # Looping columns stars left
     kiri = 0
     while kiri <= (x - bar): 
          string = string + " * " 
          kiri = kiri + 1 

     # Looping columns stars right 
     kanan = kiri 
     while kanan > 1:
		string = string + " * "
		kanan = kanan - 1

     if (bar+1) <= x: 
          string = string + "\n\n" 
          bar = bar + 1 

bar = x-1 
# Looping rows 
while bar >= 0:
     
     # Looping empty spaces
     kol = bar
     while kol > 0:
          string = string + "   "
          kol = kol - 1
		
     # Looping columns stars left	
     kiri = 1
     while kiri < (x - (bar-1)):
          string = string + " * "
          kiri = kiri + 1		

     # Looping columns stars right
     kanan = 1
     while kanan < kiri -1:
          string = string + " * "
          kanan = kanan + 1	

     string = string + "\n\n"
     bar = bar - 1
print (string)

And then, look at the results by using the command : python triangle_08.py

triangle_stars_08

With many practicing simple program that contains algorithms like this we will be much easier to analyze a problem around us. I personally hope that friends can explore more deeply this simple program. May be useful, Happy Coding!