[英]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.