打印三角形数字图案的Python程序

470 阅读1分钟

编写一个Python程序,使用for循环打印三角形数字模式。

rows = int(input("Enter Triangle Numbers Pattern Rows = " ))

print("==== 三角形数字图案====")

for i in range(1, rows + 1): for j in range(rows, i, -1): print(end = ' ') for k in range(1, i + 1): print(k, end = ' ') print()

image.png 这个Python例子使用一个while循环来打印数字的三角形图案。

# Python程序使用while循环打印数字的三角形图案 rows = int(input("Enter Rows = ")

print("========") i = 1

while(i <= rows): j = rows while(j > i): print(end = ' ') j = j - 1 k = 1 while(k <= i): print(k, end = ' ') k = k + 1 print() I = i + 1
Enter Rows = 9
========
        1 
       1 2 
      1 2 3 
     1 2 3 4 
    1 2 3 4 5 
   1 2 3 4 5 6 
  1 2 3 4 5 6 7 
 1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9