📜  Python3 |打印双面楼梯图案的程序

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

Python3 |打印双面楼梯图案的程序

下面提到的是用于打印双面楼梯案例图案的Python 3程序。

例子:

Input : 10
Output :

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

注意:此代码仅适用于 n 的偶数值。

# Python3 Program to demonstrate 
# staircase pattern
  
# function definition
def pattern(n):
  
    # for loop for rows
    for i in range(1,n+1):
  
        # conditional operator
        k =i + 1 if(i % 2 != 0) else i
  
        # for loop for printing spaces
        for g in range(k,n):
            if g>=k:
                print(end="  ")
  
        # according to value of k carry
        # out further operation
        for j in range(0,k):
            if j == k - 1:
                print(" * ")
            else:
                print(" * ", end = " ")
  
  
# Driver code
n = 10
pattern(n)

输出:

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