📜  Python|打印倒星图案

📅  最后修改于: 2022-05-13 01:56:56.279000             🧑  作者: Mango

Python|打印倒星图案

在这里,我们将打印所需尺寸的倒星图案。
例子:

1) Below is the inverted star pattern of size n=5 
  (Because there are 5 horizontal lines
   or rows consist of stars). 

   *****
    ****
     ***
      **
       *

2) Below is the inverted star pattern of size n=10 
  (Because there are 5 horizontal lines
   or rows consist of stars).

   **********
    *********
     ********
      *******
       ******
        *****
         ****
          ***
           **
            *

让我们看看Python程序打印倒星图案:

# python 3 code to print inverted star
# pattern 
  
# n is the number of rows in which
# star is going to be printed.
n=11
  
# i is going to be enabled to
# range between n-i t 0 with a
# decrement of 1 with each iteration.
# and in print function, for each iteration,
# ” ” is multiplied with n-i and ‘*’ is
# multiplied with i to create correct
# space before of the stars.
for i in range (n, 0, -1):
    print((n-i) * ' ' + i * '*')

解释:

  • 第一个行数存储在变量 n 中。
  • 然后 for 循环使 i 的范围在 ni 到 0 之间,每次迭代递减 1。
  • 之后,对于每次迭代,“”乘以 ni,“*”乘以 i,以在星星之前创建正确的空间。
  • 最后将打印所需的图案。

输出:

***********
 **********
  *********
   ********
    *******
     ******
      *****
       ****
        ***
         **
          *