Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-创建圣诞树_Python_String_Algorithm - Fatal编程技术网

Python-创建圣诞树

Python-创建圣诞树,python,string,algorithm,Python,String,Algorithm,试图用一句话打印圣诞树。下面是我的代码,它不太有效 sentence = 'The whole Christmas tree.' for word in sentence.split(): for i, char in enumerate(word, 1): # Avoid a separate counter print(9 * " ", 2 * char * i) 期望输出: TT hhhh eeeeee w

试图用一句话打印圣诞树。下面是我的代码,它不太有效

sentence = 'The whole Christmas tree.'
for word in sentence.split():
    for i, char in enumerate(word, 1): # Avoid a separate counter
        print(9 * " ", 2 * char * i)
期望输出:

        TT
       hhhh
      eeeeee
        ww
       hhhh
      oooooo
     llllllll
    eeeeeeeeee
        CC
       hhhh
      rrrrrr
     iiiiiiii
    ssssssssss
   tttttttttttt
  mmmmmmmmmmmmmm
 aaaaaaaaaaaaaaaa
ssssssssssssssssss
        tt
       rrrr
      eeeeee
     eeeeeeee
    ..........

有一件事,你们在这里明显地遗漏了,那就是适当的偏移量。 整棵树中最长的一行将是“圣诞节”中“s”的那一行。它将有18个字符长。要创建相等的偏移量,您需要精确地添加
(18-lengthOfRow)//2
空格。请看下面的代码:

sentence = 'The whole Christmas tree.'
for word in sentence.split():
    for i in range(len(word)):
        # For each word letter
        letters = word[i] * 2 * (i+1)
        # Add equal offset
        offset = " " * ((18 - len(letters)) // 2)
        print(offset + letters)
它产生:

        TT
       hhhh
      eeeeee
        ww
       hhhh
      oooooo
     llllllll
    eeeeeeeeee
        CC
       hhhh
      rrrrrr
     iiiiiiii
    ssssssssss
   tttttttttttt
  mmmmmmmmmmmmmm
 aaaaaaaaaaaaaaaa
ssssssssssssssssss
        tt
       rrrr
      eeeeee
     eeeeeeee
    ..........
这个神奇的数字是句子中最长单词的长度乘以2。如果您想更改句子,可以通过以下方式找到正确的数字:

offset = 2 * len(sorted(sentence.split(), key=len)[-1])

使用
'>'>'
格式化字符串语法以强制

对于python 3.5

print(f'{char*i:>9}{char*i}')
这里首字母“9”的目的是什么?它只是添加了一个填充物,如果你不需要它,你可能需要它

我假设打印的第一个参数与树的中心位置“减去”循环couter(即I)相同。因此,这样做可能会奏效:

print((x - i) * " ", 2 * char * i)
其中x是:

len(sentence) / 2

您还可以尝试这个使用python字符串格式的解决方案,这样就不必担心偏移量

要了解有关字符串格式的更多信息,请阅读


我注意到这部分代码
打印(9*“”,2*char*I)
。这将导致您只打印一半的圣诞树

您可以将其替换为:
print(((len(句子)//2)-i)*'',2*char*i)


这将为您提供所需的输出

您很接近了。首先要打印一个元组。相反,您希望连接空格和字符字符串并打印结果字符串。第二,在每行中打印恒定数量的前导空格。数字应随行而异。如果检查所需的输出,您将看到树中心的字符数始终需要等于最长单词的长度。您希望在发出大量空格后开始打印非空格字符,以便它们加上一半的非空格字符到达中心线

以尽可能接近您已有的方式对这些想法进行编码

sentence = 'The whole Christmas tree.'
words = sentence.split()
center = max(len(w) for w in words)
for word in words:
    for i, char in enumerate(word, 1):
        print(' ' * (center - i) + 2 * char * i)

这适用于任何句子。

您得到了什么输出?也许可以帮助您。我得到了不同的输出。
sentence = 'The whole Christmas tree.'

word = []
for item in sentence:
    if ' ' not in item:
        word.append(item)
    else:
        for ind, inner in enumerate(word):
            print('{:^20}'.format(''.join([inner for _ in range(2 * (ind + 1))])))
        word = []
sentence = 'The whole Christmas tree.'
words = sentence.split()
center = max(len(w) for w in words)
for word in words:
    for i, char in enumerate(word, 1):
        print(' ' * (center - i) + 2 * char * i)