[How to create] – Dynamic Number Triangle With Python

Previously we’ve tried to Make Dynamic Stars Triangle With Python. Now we try to go back modify the output in the form of numbers, either increment or decrement.

  1. For the first, we make a triangle with the shape of the bottom left alignment increment numbers. Then create a file name triangle_numbers_01.py and save them in your directory.
string = ""
bar = 1

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

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

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

segitiga_angka_kiri_bawah

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

string = ""

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

     # Looping columns
     kol = bar
     while kol > 0:
          string = string + " " + str(no) + " "
          kol = kol - 1
		
     string = string + "\n"
     bar = bar - 1
     no = no - 1
print (string)

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

segitiga_angka_kiri_atas

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

string = ""

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

     # Looping right columns numbers
     kanan = 1
     while kanan < (x - (bar-1)):
          string = string + " " + str(no) + " "
          kanan = kanan + 1		
		
     string = string + "\n"
     bar = bar - 1
     no = no + 1
print (string)

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

segitiga_angka_kanan_bawah

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

string = ""
bar = 1

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

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

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

segitiga_angka_kanan_atas

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

string = ""

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

     # Looping left columns numbers
     kiri = 1
     while kiri < (x - (bar-1)):
          string = string + " " + str(no) + " "
          kiri = kiri + 1		

     # Looping right columns numbers
     kanan = 1
     while kanan < kiri -1:
          string = string + " " + str(no) + " "
          kanan = kanan + 1	

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

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

segitiga_angka_tengah_bawah

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

string = ""
bar = 1

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

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

     # Looping left columns numbers
     kiri = 0
     while kiri <= (x - bar): 
          string = string + " " + str(no) + " " 
          kiri = kiri + 1 
     
     # Looping right columns numbers 
     kanan = kiri 
     while kanan > 1:
          string = string + " " + str(no) + " "
          kanan = kanan - 1

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

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

segitiga_angka_tengah_atas

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

string = ""

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

	# Looping left columns numbers
	kiri = 1
	while kiri < (x - (bar-1)):
		string = string + " " + str(no) + " "
		kiri = kiri + 1		

	# Looping right columns numbers
	kanan = 1
	while kanan < kiri -1:
		string = string + " " + str(no) + " "
		kanan = kanan + 1	

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

no = no -2
# Looping rows
bar = 1	
while bar <= x: 

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

     # Looping left columns numbers
     kiri = 0
     while kiri < (x - bar): 
          string = string + " " + str(no) + " " 
          kiri = kiri + 1 

     # Looping right columns numbers
     kanan = kiri 
     while kanan > 1:
          string = string + " " + str(no) + " "
          kanan = kanan - 1

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

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

segitiga_angka_tengah_belah_ketupat

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

string = ""
bar = 1

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

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

     # Looping left columns numbers
     kiri = 0
     while kiri <= (x - bar): 
          string = string + " " + str(no) + " " 
          kiri = kiri + 1 

     # Looping right columns numbers 
     kanan = kiri 
     while kanan > 1:
          string = string + " " + str(no) + " "
          kanan = kanan - 1

     if (bar+1) <= x: 
          string = string + "\n\n" 
          bar = bar + 1 
no = no -1 # 
Looping rows 
bar = x-1 
while bar >= 0:

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

     # Looping left columns numbers	
     kiri = 1
     while kiri < (x - (bar-1)):
          string = string + " " + str(no) + " "
          kiri = kiri + 1		

     # Looping right columns numbers
     kanan = 1
     while kanan < kiri -1:
          string = string + " " + str(no) + " "
          kanan = kanan + 1	

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

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

segitiga_angka_tengah

 

[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!