[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