用于打印菱形的 Python 程序

pythonserver side programmingprogramming

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)

运行上述代码得到以下结果:

           @
          @@@
         @@@@@
        @@@@@@@
       @@@@@@@@@  
      @@@@@@@@@@@
     @@@@@@@@@@@@@
    @@@@@@@@@@@@@@@
     @@@@@@@@@@@@
     @@@@@@@@@@@
      @@@@@@@@@
       @@@@@@@
       @@@@@
        @@@
         @


相关文章