Python程序打印钻石形状
python中的循环功能可用于使用键盘上的各种字符来创建许多格式良好的图表。一种这样的形状是菱形,其将涉及多个环。这是因为我们必须垂直和水平打印字符。另外,我们还必须注意从顶部到中间逐渐增大然后从中间到底部逐渐缩小的形状。因此,我们将使用两个for循环,每个循环中都包含一个for循环。
下面是创建菱形形状的代码。
示例
def Shape_of_Diamond(shape):
a = 0
for m in range(1, shape + 1):
for n in range(1, (shape - m) + 1):
print(end=" ")
while a != (2 * m - 1):
print("@", end="")
a = a + 1
a = 0
print()
s = 1
c = 1
for m in range(1, shape):
for n in range(1, s + 1):
print(end=" ")
s = s + 1
while c <= (2 * (shape - m) - 1):
print("@", end="")
c = c + 1
c= 1
print()
shape = 8
Shape_of_Diamond(shape)运行上面的代码将为我们提供以下结果:
@
@@@
@@@@@
@@@@@@@
@@@@@@@@@
@@@@@@@@@@@
@@@@@@@@@@@@@
@@@@@@@@@@@@@@@
@@@@@@@@@@@@
@@@@@@@@@@@
@@@@@@@@@
@@@@@@@
@@@@@
@@@
@