编写一个 Python 程序,使用 For Loop 和 While Loop 打印数字倒直角三角形,并附带示例。
Python 使用 For Loop 打印倒直角三角形的程序
此 Python 程序允许用户输入总行数。接下来,我们使用 Python While Loop 和 For Loop 将数字从最大值打印到 1,形成倒直角三角形。
# Python Program to Print Inverted Right Triangle of Numbers
rows = int(input("Please Enter the total Number of Rows : "))
print("Inverted Right Triangle Pattern of Numbers")
i = rows
while(i >= 1):
for j in range(1, i + 1):
print('%d ' %i, end = ' ')
i = i - 1
print()
Please Enter the total Number of Rows : 12
Inverted Right Triangle Pattern of Numbers
12 12 12 12 12 12 12 12 12 12 12 12
11 11 11 11 11 11 11 11 11 11 11
10 10 10 10 10 10 10 10 10 10
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
>>>
Python 使用 While Loop 打印倒直角三角形程序
此 Python 倒直角三角形程序与上述程序相同。但是,在此 Python 程序中,我们将 For Loop 替换为 While Loop。
# Python Program to Print Inverted Right Triangle of Numbers
rows = int(input("Please Enter the total Number of Rows : "))
print("Inverted Right Triangle Pattern of Numbers")
i = rows
while(i >= 1):
j = 1
while(j <= i):
print('%d ' %i, end = ' ')
j = j + 1
i = i - 1
print()
