使用兩個嵌套循環來處理級別和每個級別內的變化,並使用該str.center
方法將每一行居中。
>>> def christmas_tree(h, n, s):
... max_width = 1 + 2 * h * n
... width = 1
... for _ in range(n):
... for _ in range(h):
... print(("*" * width).center(max_width))
... width += 2
... width -= 2 * s
...
>>> christmas_tree(5, 4, 2)
*
***
*****
*******
*********
*******
*********
***********
*************
***************
*************
***************
*****************
*******************
*********************
*******************
*********************
***********************
*************************
***************************
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句