How to print a pyramid of stars using Python?

You can use loop statements to print a pyramid of stars. Here is an example code:

def print_pyramid(rows):
    for i in range(rows):
        print(' '*(rows-i-1) + '*'*(2*i+1))

# 输入金字塔的行数
num_rows = int(input("请输入金字塔的行数:"))

# 调用函数打印金字塔
print_pyramid(num_rows)

After running the program, it will prompt for the number of rows in the pyramid, then print out the corresponding number of rows of a star pyramid.

bannerAds