Python Forum

Full Version: Merry Xmas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
"""
xmas_tree.py

Prints a tree.

Functions:
tree: Create a text representation of a tree. (str)
"""


from __future__ import print_function

import sys


def tree(width):
    """
    Create a text representation of a tree. (str)

    Parameters:
    width: How wide the bottom of the tree should be. (int)
    """
    # Make sure you have an odd width.
    if width % 2 == 0:
        width += 1
    # Get the main section of the tree.
    lines = ['']
    for row_width in range(1, width + 1, 2):
        lines.append(('*' * row_width).center(width))
    # Print an appropriately sized trunk.
    trunk_size = 3 if width > 13 else 1
    for row in range(trunk_size):
        lines.append(('H' * trunk_size).center(width))
    return '\n'.join(lines)


if __name__ == '__main__':
    # This tree goes to 11.
    if len(sys.argv) > 1:
        width = int(sys.argv[1])
    else:
        width = 11
    print(tree(width))
Merry Xmas to you as well.
def christmas(word, numb):
    return f'  \N{tear-off calendar}  '.join([f'{word.capitalize():\N{Christmas tree}^21}'] * numb)

print(christmas('merry christmas', 2))
[Image: DIStA7.png]