【问题标题】:How do I align these two shapes to form a tree?如何对齐这两个形状以形成一棵树?
【发布时间】:2018-10-09 10:25:43
【问题描述】:

我需要打印一个用户输入 4 个不同参数的树形。枝高、枝宽、茎高和茎宽。我有两个形状,形成树的顶部和底部,但我似乎无法弄清楚如何将它们放在一起,使其看起来像一棵树。我想我需要计算分支的宽度并从中扣除茎,但我不确定。我的输出目前如下所示:

有什么建议吗?

Enter height of the branches: 5
     *
    ***
   *****
  *******
 *********
Enter width of the stem: 5
*****
*****
*****
*****
*****



def pyramid(height):
    for row in range(height):
        for count in range(height - row):
            print(end=" ")
        for count in range(2 * row + 1):
            print(end="*")
        print()

def square(width):
    for i in range(width):
        for j in range(width):
            print('*', end='')
        print()


height = int(input("Enter height of the branches: "))
pyramid(height)

width = int(input("Enter width of the stem: "))
square(width)

【问题讨论】:

  • 要对齐词干,您需要在每行的开头添加一些空格。多少空间取决于树枝的宽度,而树枝的宽度又取决于树的高度。试着弄清楚高宽的关系(1级->1宽,2级->3宽等);每次你加两个*,所以应该是双倍吧?然后,您需要放入词干的边距是该宽度的一半,减去词干本身宽度的一半,因此词干的中心最终位于中间。请注意,在这种情况下,只有奇数大小的词干可以完全居中。
  • 谢谢!我注意到只有当数字是奇数时它才会完全居中。
  • 您应该在其中添加一些错误检查,即高度为奇数且宽度小于2 * height + 1 等 :)

标签: python python-3.x


【解决方案1】:

可以试试这个:

    def pyramid(height):
        for row in range(height):
            for count in range(height - row):
                print(end=" ")
            for count in range(2 * row + 1):
                print(end="*")
            print()

    def square(width):
        if height % 2 == 0:
         space=int((height/2))*' '
        else:
         space=int((height/2)+1)*' '
        for i in range(width):
            print(end=space)
            for j in range(width):
                print('*', end='')
            print()


    height = int(input("Enter height of the branches: "))


    width = int(input("Enter width of the stem: "))
    pyramid(height)
    square(width)

【讨论】:

    【解决方案2】:

    您正在寻找str.center(width\[, fillchar\]):

    def pyramid(height):
        for row in range(height):
            print(('*' * (2 * row + 1)).center((2 * height + 1)))
    
    def square(width, height):
        for i in range(width):
            print(('*' * (width)).center((2 * height + 1)))
    
    height = int(input("Enter height of the branches: "))
    pyramid(height)
    
    width = int(input("Enter width of the stem: "))
    square(width, height)
    

    输出:

    C:\_\Python363-64\python.exe C:/Users/MrD/.PyCharm2018.2/config/scratches/scratch_75.py
    Enter height of the branches: 5
         *     
        ***    
       *****   
      *******  
     ********* 
    Enter width of the stem: 5
       *****   
       *****   
       *****   
       *****   
       *****   
    
    Process finished with exit code 0
    

    【讨论】:

      【解决方案3】:

      您可以在茎的每一行之前添加足以填充金字塔高度减去茎宽度一半的空格:

      def pyramid(height):
          for row in range(height):
              for count in range(height - row):
                  print(end=" ")
              for count in range(2 * row + 1):
                  print(end="*")
              print()
      
      def square(width, pyramid_height):
          for i in range(width):
              print(' ' * (pyramid_height - width // 2), end='')
              for j in range(width):
                  print('*', end='')
              print()
      
      
      height = int(input("Enter height of the branches: "))
      pyramid(height)
      
      width = int(input("Enter width of the stem: "))
      square(width, height)
      

      【讨论】:

      • str.center 是一种开箱即用的方式:)
      猜你喜欢
      • 1970-01-01
      • 2019-12-12
      • 2012-01-06
      • 1970-01-01
      • 2021-12-13
      • 2012-01-12
      • 2022-11-19
      • 1970-01-01
      • 2022-01-25
      相关资源
      最近更新 更多