Python 如何用while创建圣诞树?

Python 如何用while创建圣诞树?,python,Python,我目前正在编写一些代码,我是一名研究检疫的学生,我正试图解决一个圣诞树的问题,但不能完全理解它 圣诞树必须用“while”完成,我已经试过了,但我只得到了一半 代码行: lines=1 maxlines=9 while lines>=maxlines: print (lines*'*') lines+=1 我得到的是: * ** *** **** ***** ****** ******* ******** ********* 我想要的是: *

我目前正在编写一些代码,我是一名研究检疫的学生,我正试图解决一个圣诞树的问题,但不能完全理解它

圣诞树必须用“while”完成,我已经试过了,但我只得到了一半

代码行:

lines=1
maxlines=9
while lines>=maxlines:
  print (lines*'*')
  lines+=1
我得到的是:

*
**
***
****
*****
******
*******
********
*********
我想要的是:

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

给你。这将打印树

def tree(n):

    # total spaces
    k = 2 * n - 2

    # number of rows
    i = 0
    while i < n:
        j = 0

        # inner loop for number spaces
        while j < k:
            print(end=" ")
            j = j + 1

        # decrementing k after loop
        k = k - 1

        # number of columns
        j = 0
        while j < i + 1:
            print("* ", end="")
            j = j + 1

        # end line
        print("\r")
        i = i + 1


# Begining
n = 5
tree(n)
def树(n):
#总空间
k=2*n-2
#行数
i=0
而i
星号=1#星号计数
最大线=9#总线数
实际行数=0#行数

首先,当实际值时,代码无法工作,因为在while循环中,
永远不会大于
最大行
,因此语句为
False

正如所有其他人提到的,您缺少空格。另一种尽可能接近代码的方法是:

行=1
最大线=9

而行应该是什么树的其余部分?编辑我想要得到的,类似这样的东西。所需的输出在星号之前有空格-您是否尝试在打印的内容中星号之前有空格?不,不是真的。。。我似乎不明白,我已经有一段时间没工作了。我还是个新手,这正是我想要的。谢谢你的帮助!如果它解决了你的问题,请接受答案。请随时询问有关上述代码的任何问题。完成!再次感谢。
star = 1 # Star count
maxLines = 9 # Total number of lines
actualLines = 0 # Lines count

while actualLines <= maxLines:

     # print the necessary spaces before the stars   print the stars
     print(str(abs(maxLines - actualLines ) * ' ') + str(star * '*'))

     star += 2 # add 2 stars every new line
     actualLines += 1 # add 1 to the line counting
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************