简体   繁体   English

制作树形。

[英]Making a tree shape.

I'm trying to print something that looks like this: 我正在尝试打印如下内容:

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

with the user inputting the width of the thickest part of the head and the width of the stem. 用户输入头部最厚部分的宽度和杆的宽度。

So far I have managed to get the head to print using this code: 到目前为止,我已经设法使用以下代码进行打印:

def head(size):
    n=1
    while n < size+1:
        astri = n * "*"
        print '{:^50}'.format(astri)
        n += 2

 print head(x)

x = input("Please enter an odd integer for the head")

But I'm completely stuck on how to do the stem of the tree. 但是我完全坚持如何做树的茎。

Something like this: 像这样:

def tree(head, stem):
    #for head
    for i in xrange(1, head+1, 2):
        print '{:^{}}'.format('*'*i, head)
    #for trunk
    for _ in xrange(3):
        print '{:^{}}'.format('*'*stem, head)
...         
>>> tree(10, 3)
    *     
   ***    
  *****   
 *******  
********* 
   ***    
   ***    
   ***    
>>> tree(5, 1)
  *  
 *** 
*****
  *  
  *  
  *  

Update: 更新:

To keep the width of stem in proportion to width of head : 使stem宽度与head宽度成比例:

def tree(head, stem):
    for i in xrange(1, head+1, 2):
        print ('*'*i).center(head)
    x = (head/2) if (head/2)%2 else (head/2)-1
    for _ in xrange(stem):
        print ('*'*x).center(head)

>>> tree(12, 2)
     *      
    ***     
   *****    
  *******   
 *********  
*********** 
   *****    
   *****    
>>> tree(14, 4)
      *       
     ***      
    *****     
   *******    
  *********   
 ***********  
************* 
   *******    
   *******    
   *******    
   *******    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python fsolve()抱怨形状。 为什么? - Python fsolve() complains about shape. Why? TFLearn“无法提供形状的值”。 - TFLearn “cannot feed value of shape.” 基于形状的numpy数组的组列表。 熊猫? - Group list of numpy arrays based on shape. Pandas? pytorch 关于张量形状的问题。 以及如何重塑张量 - pytorch question about tensor shape. And how to reshape a tensor “数组形状不正确。” 从npy转换为binaryproto时出错 - 'Incorrect array shape.' error in converting from npy to binaryproto ValueError:形状不匹配:无法将对象广播到单个形状。 不匹配在 arg 0 与形状 (48000,) 和 arg 1 与形状 (2,) 之间 - ValueError: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (48000,) and arg 1 with shape (2,) Fish Simulator 鱼的生长方式快速和变形。 如何修复 Pygame - Fish Simulator Fish Growing Way To Fast And Growing Out Of Shape. How To Fix Pygame 如何制作成Pascal三角形形状。 在第一行中表示1,然后在第二行中表示1,1,在第三行中表示1,2,1 - How to make it in Pascal triangle shape. Meaning 1 in first row, and then 1,1 in second row, 1,2,1 in third LSTM 数据形状。 我需要帮助更改 LSTM 以读取我的 DataFrame(反之亦然) - LSTM data shape. I need help changing the LSTM to read my DataFrame (or vice versa) 制定决策树 - Making a decision tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM