Python 打印右侧帕斯卡星形三角形程序

编写一个Python程序,使用for循环打印右侧帕斯卡星形三角形。

rows = int(input("Enter Right Pascals Star Triangle Pattern Rows = "))

print("====Right Pascals Star Triangle Pattern====")

for i in range(0, rows):
    for j in range(0, i + 1):
        print('*', end = ' ')
    print()

for i in range(rows - 1, -1, -1):
    for j in range(0, i):
        print('*', end = ' ')
    print()
Python Program to Print Right Pascals Star Triangle

此Python程序使用while循环打印右侧帕斯卡星形三角形。

rows = int(input("Enter Right Pascals Star Triangle Pattern Rows = "))

print("====Right Pascals Star Triangle Pattern====")
i = 0
while(i < rows):
    j = 0
    while(j <= i):
        print('*', end = ' ')
        j = j + 1
    print()
    i = i + 1

i = rows - 1
while(i >= 0):
    j = 0
    while(j <= i - 1):
        print('*', end = ' ')
        j = j + 1
    print()
    i = i - 1
Enter Right Pascals Star Triangle Pattern Rows = 7
====Right Pascals Star Triangle Pattern====
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

在此Python示例中,我们使用pyRightPascalStar函数来显示给定字符的右侧帕斯卡三角形图案。

def pyRightPascalsStarTriangle(rows, ch):
    for i in range(0, rows):
        for j in range(0, i + 1):
            print('%c' %ch, end = ' ')
        print()

    for i in range(rows - 1, -1, -1):
        for j in range(0, i):
            print('%c' %ch, end = ' ')
        print()
    
rows = int(input("Enter Right Pascals Star Triangle Pattern Rows = "))

ch = input("Symbol to use in Right Pascals Star Triangle Pattern = " )

print("====Right Pascals Star Triangle Pattern====")
pyRightPascalsStarTriangle(rows, ch)
Enter Right Pascals Star Triangle Pattern Rows = 10
Symbol to use in Right Pascals Star Triangle Pattern = $
====Right Pascals Star Triangle Pattern====
$ 
$ $ 
$ $ $ 
$ $ $ $ 
$ $ $ $ $ 
$ $ $ $ $ $ 
$ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ 
$ $ $ $ $ $ 
$ $ $ $ $ 
$ $ $ $ 
$ $ $ 
$ $ 
$