您想用 Python 构建一个金字塔模式,其中金字塔的每一行都由星号 (*) 组成。
解决方案: 您可以通过使用循环在每一行中打印空格和星号来构建金字塔图案。
下面是一个创建金字塔图案的 Python 代码示例:
def build_pyramid (height):
for i in range (1, height + 1):
spaces = ' ' * (height - i)
stars = '*' * (2 * i - 1)
print (spaces + stars)
# Input: Specify the height of the pyramid
pyramid_height = 5
# Build and display the pyramid
build_pyramid(pyramid_height)
# Output:
*
***
*****
*******
*********
您可以自定义 pyramid_height 变量,为您的金字塔图案设置所需的高度。本代码提供了在 Python 中使用星号构建金字塔图案的解决方案。