编写一个Python程序,使用For循环和While循环打印右三角数字图案,并附带示例。
Python 使用 For 循环打印右三角数字图案程序
这个 Python 程序允许用户输入总行数。接下来,我们使用嵌套的 For 循环打印从 1 到最大值(用户指定的行数)的右三角数字。
rows = int(input("Please Enter the total Number of Rows : "))
print("Right Triangle Pattern of Numbers")
for i in range(1, rows + 1):
for j in range(1, i + 1):
print('%d' %i, end = ' ')
print()

Python 使用 While 循环打印右三角数字图案程序
这个 Python 右三角数字程序与上面的相同。但是,在这个 Python 程序中,我们用 While 循环 替换了 For 循环。
rows = int(input("Please Enter the total Number of Rows : "))
print("Right Triangle Pattern of Numbers")
i = 1
while(i <= rows):
j = 1
while(j <= i):
print('%d' %i, end = ' ')
j = j + 1
i = i + 1
print()
右三角数字图案输出
Please Enter the total Number of Rows : 10
Right Triangle Pattern of Numbers
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10