Python - create a Christmas Tree

Viewed 697

Trying to print a Christmas tree using a sentence. Below is my code which does not quite work.

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)

desired output:

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

You're close. First you're printing a tuple. Instead you want to concatenate the strings of spaces and characters and print the resulting string. Second, you're printing a constant number of leading spaces in each line. The number should vary by line. If you examine the desired output, you'll see that the number of characters to the center of the tree always needs to be equal to the length of the longest word. You want to starting printing non-space characters after emitting a number of spaces such that they plus half the non-space characters get to the center line.

Coding these ideas in a way as close as possible to what you already have,

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)

This will work fine with any sentence.

One thing that you are clearly missing here is proper offset. The longest row in the whole tree will be the one from 's' in 'Christmas'. It will be 18 characters long. To create an equal offset you want to exactly add (18 - lengthOfRow)//2 empty spaces. Take a look at the code below:

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)

It produces:

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

This magic 18 used there is the length of the longest word in the sentence, multiplied by a factor of 2. If you would like to change the sentence, you can find the right number in this way:

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

Format String Syntax with '>' to force the field to be right-aligned

For python <=3.5

print('{0: >9}{1}'.format(char * i, char * i))

For python >3.5

print(f'{char * i: >9}{char * i}')
print(9 * " ", 2 * char * i)

What is the purpose of initial "9" here? It is just adding a padding may be you need it, if not get rid of it.

I would assume the first argument to print to be same the center position of the tree "minus" the loop couter which is i. So may be something like this will work:

print((x - i) * " ", 2 * char * i)

Where x is:

len(sentence) / 2

You can also try this solution which uses python's string formatting, so you don't need to worry about the offset.

To learn more about string formatting, you can read this.

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 = []

I noticed this part of the code print(9 * " ", 2 * char * i). This will cause you to only print half of your christmas tree.

You can replace that with this: print(((len(sentence) // 2) - i) * " ", 2 * char * i)

This will get you the desired output you want

Related