我刚在我的大学开始了 python 课程,需要帮助解决一个练习。它要求我写一个 Christmas_tree。
1 回答
0
使用两个嵌套循环来处理级别和每个级别内的更改,并使用该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)
*
***
*****
*******
*********
*******
*********
***********
*************
***************
*************
***************
*****************
*******************
*********************
*******************
*********************
***********************
*************************
***************************
于 2021-11-16T17:34:51.953 回答