使用星号、数字和字符打印三角形的程序
示例 1:使用星号打印半金字塔的程序
* * * * * * * * * * * * * * *
源代码
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print()
在上面的程序中,让我们看看图案是如何打印的。
- 首先,我们从用户那里获取金字塔的高度
rows
。 - 在第一个循环中,我们从
i = 0
迭代到i = rows
。 - 第二个循环从 j = 0 运行到 i + 1。在这个循环的每次迭代中,我们不换行地打印
i + 1
个*
。这里,行号给出了在该行上需要打印的*
的数量。例如,在第 2 行,我们打印两个*
。同样,在第 3 行,我们打印三个*
。 - 内层循环结束后,我们打印新行并开始在新行中打印 *。
另请阅读
示例 2:使用数字打印半金字塔的程序
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
源代码
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print(j+1, end=" ")
print()
在上面的程序中,让我们看看图案是如何打印的。
- 首先,我们从用户那里获取金字塔的高度
rows
。 - 在第一个循环中,我们从
i = 0
迭代到i = rows
。 - 在第二个循环中,我们打印从
1
到j
的数字,其中j
的范围是0
到i
。 - 在第一个循环的每次迭代之后,我们打印一个新行。
示例 3:使用字母打印半金字塔的程序
A B B C C C D D D D E E E E E
源代码
rows = int(input("Enter number of rows: "))
ascii_value = 65
for i in range(rows):
for j in range(i+1):
alphabet = chr(ascii_value)
print(alphabet, end=" ")
ascii_value += 1
print()
上面示例的工作原理也与上面讨论的其他示例类似,只是这里打印的是 ASCII 值。字母的 ASCII 值从 65 (即 A) 开始。因此,在每次迭代中,我们增加 ascii_value
的值并打印其对应的字母。
另请阅读
使用星号和数字打印倒半金字塔的程序
示例 4:使用星号的倒半金字塔
* * * * * * * * * * * * * * *
源代码
rows = int(input("Enter number of rows: "))
for i in range(rows, 0, -1):
for j in range(0, i):
print("* ", end=" ")
print()
此示例类似于正向金字塔,只是这里我们从 rows
的总数开始,并且在每次迭代中我们将 rows
的数量减 1。
示例 5:使用数字的倒半金字塔
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
源代码
rows = int(input("Enter number of rows: "))
for i in range(rows, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()
使用数字的正向金字塔和倒向金字塔之间唯一的区别是第一个循环从 rows
的总数到 0。
打印完整金字塔的程序
示例 6:使用星号打印完整金字塔的程序
* * * * * * * * * * * * * * * * * * * * * * * * *
源代码
rows = int(input("Enter number of rows: "))
k = 0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k += 1
k = 0
print()
这种类型的金字塔比我们上面研究的要复杂一些。
- 最外层循环从
i = 1
迭代到i = row + 1
。 - 在两个内层循环中,for 循环使用公式
(rows-i)+1
打印每行所需的空格,其中 rows 是总行数,i
是当前行号。 - while 循环使用公式
2 * i - 1
打印所需数量的星号。此公式给出每行的星号数量,其中行是i
。
示例 7:数字的完整金字塔
1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5
源代码
rows = int(input("Enter number of rows: "))
k = 0
count=0
count1=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(" ", end="")
count+=1
while k!=((2*i)-1):
if count<=rows-1:
print(i+k, end=" ")
count+=1
else:
count1+=1
print(i+k-(2*count1), end=" ")
k += 1
count1 = count = k = 0
print()
与示例 6 类似,此示例也使用了 for 循环中的两个循环。
- 外层 for 循环遍历每一行。
- 这里我们使用两个计数器
count
和count1
分别用于打印空格和数字。 - 内层 for 循环使用公式
(rows-i)+1
打印所需的空格,其中 rows 是总行数,i
是当前行号。 - while 循环打印数字,其中
2 * i - 1
给出每行的项目数。
示例 8:倒置的星号完整金字塔
* * * * * * * * * * * * * * * * * * * * * * * * *
源代码
rows = int(input("Enter number of rows: "))
for i in range(rows, 1, -1):
for space in range(0, rows-i):
print(" ", end="")
for j in range(i, 2*i-1):
print("* ", end="")
for j in range(1, i-1):
print("* ", end="")
print()
在此示例中,我们总共使用了 4 个 for 循环。
- 外层 for 循环从
i = rows
迭代到i = 1
。 - 第一个内层 for 循环打印每行所需的空格。
- 第二个内层 for 循环打印金字塔的前半部分(垂直切割),而最后一个内层 for 循环打印另一半。
示例 9:帕斯卡三角形
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
源代码
rows = int(input("Enter number of rows: "))
coef = 1
for i in range(1, rows+1):
for space in range(1, rows-i+1):
print(" ",end="")
for j in range(0, i):
if j==0 or i==0:
coef = 1
else:
coef = coef * (i - j)//j
print(coef, end = " ")
print()
在此示例中,我们使用了三个 for 循环。
- 外层循环从
1
迭代到rows + 1
。 - 第一个内层循环打印空格。
- 第二个内层循环首先使用语句
coef = coef * (i - j) // j
找到要打印的数字,然后打印它。这里,i
是行号,j
是从0
到i
的值。
示例 10:弗洛伊德三角形
1 2 3 4 5 6 7 8 9 10
源代码
rows = int(input("Enter number of rows: "))
number = 1
for i in range(1, rows+1):
for j in range(1, i+1):
print(number, end=" ")
number += 1
print()
这是最简单的图案之一。
number
变量初始化为值 1。- 外层 for 循环从 1 迭代到总行数。
- 内层 for 循环从
1
迭代到i + 1
,其中 i 是行号。每次迭代后,number
的值都会增加。