Python 打印 K 形数字图案程序

编写一个 Python 程序,使用 for 循环打印 K 形数字图案。

rows = int(input("Enter K Shape Number Pattern Rows = "))

print("====K Shape Number Pattern====")

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

for i in range(2, rows + 1):
    for j in range(1, i + 1):
        print(j, end = ' ')
    print()
Python Program to Print K Shape Number Pattern

这个示例程序使用 while 循环打印 K 形数字图案。

rows = int(input("Enter Rows = "))

print("====K Shape Number Pattern====")
i = rows

while(i >= 1):
j = 1
while(j <= i):
print(j, end = ' ')
j = j + 1
print()
i = i - 1

i = 2
while(i <= rows ):
j = 1
while(j <= i):
print(j, end = ' ')
j = j + 1
print()
i = i + 1
Enter Rows = 9
====K Shape Number Pattern====
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
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