【问题标题】:How to print a pyramid of full pyramids?如何打印完整金字塔的金字塔?
【发布时间】:2021-10-30 11:20:52
【问题描述】:

我是一名新手程序员,我们最近的任务是打印一个完整的星号金字塔,其中行数基于输入,然后打印这些金字塔的金字塔。

当用户输入Number of Rows: 4 时,我期望的输出基本上是:

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

这是我可以使用循环处理的最好的。

rowInput = int(input("Number of Rows: "))
rowCount = 0
min = rowInput
max = rowInput

while (rowCount < rowInput):
  digit = 1
  while (digit < min): 
    print(end=" ")
    digit += 1
  while (digit >= min and digit <= max):
    print(end="*")
    digit += 1
    
  print()
  min -= 1
  max += 1
  rowCount += 1

输入:

Number of Rows: 4

输出:

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

我认为它可以与顶部的另一个循环一起使用,但我想不出一种可行的方法来增加两个三角形之间的空间。还有其他选择吗?

【问题讨论】:

  • 一些可以提供帮助的python提示:您可以将字符串相乘3*'*'返回'***',也可以使用空格。 range 类型会让你的生活更轻松

标签: python loops design-patterns


【解决方案1】:

您可以使用嵌套列表推导构建要打印的行列表,但这有点复杂。

rows = 4

lines = [" "*(rows-r//2-1)*(2*rows-1) +  f"{'*'*i:^{2*rows-1}}"*r
         for r in range(1,2*rows+1,2)
         for i in range(1,2*rows+1,2)]

print(*lines,sep='\n')

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

为了更容易理解代码,for-loops 可能更好:

rows = 4

width = 2*rows-1                          # width of one pyramid
for r in range(1,rows+1):                 # row with r pyramids
    indent = " "*(rows-r)*width           # indent pyramids
    for p in range(rows):                 # pyramid lines 
        fill = "*"*(2*p+1)                # filled width/stars for line
        print(indent+(2*r-1)*f"{fill:^{width}}") # repeated pyramid stars

解决此问题的另一个好方法是将问题分解为您在单个函数中实现的较小部分。结合执行小任务的功能可以更轻松地实施和测试您的解决方案:

# produce lines for one pyramid
def pyramid(n):
    return [f"{'*'*r:^{2*n-1}}" for r in range(1,2*n+1,2)]

# combine pyramids horizontally
def pyramidRow(count,n): 
    return [count*line for line in pyramid(n)]

width = 2*rows-1                          # width of one pyramid
for r in range(1,rows+1):                 # row of r pyramids
    indent = " "*width*(rows-r)           # indent pyramids 
    for line in pyramidRow(2*r-1,rows):   # print repeated pyramids
        print(indent+line)

测试:

for line in pyramid(4): print(line)

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

for line in pyramidRow(3,4): print(line)

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

【讨论】:

  • (2*r-1)*f"{fill:^{width}} 请问这部分代码是什么意思?
  • 2*r-1r 行上的金字塔数。这个数字乘以一个金字塔线的空格+星号+空格字符串,基本上可以水平打印多次。 f"{fill:^{width}}" 部分是使用 Python 的格式字符串约定构建的空格+星号+空格字符串,其中fill 是该行上的金字塔所需的重复星号,:^{width} 指定通过添加空格将这些星号以固定宽度居中在每一边。
  • 另一个使用字符串format的好帖子。
猜你喜欢
  • 2018-06-05
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2019-09-14
相关资源
最近更新 更多