Python Projects

Merry Christmas using Python 🧡

Code : 

from colorama import Fore

def heart_shape(msg=”Merry Christmas”):

    lines = []

    for y in range(15, -15, -1):

        line = “”

        for x in range(-30, 30):

            f = ((x * 0.05) ** 2 + (y * 0.1) ** 2 – 1) ** 3 – (x * 0.05) ** 2 * (y * 0.1) ** 3

            line += msg[(x – y) % len(msg)] if f <= 0 else ” “

        lines.append(line)

    print(Fore.RED+”\n”.join(lines))

    print(Fore.GREEN+msg)

heart_shape()  # Call the function to create the heart

Explnation of the code in details :

This code generates a text-based heart shape using the Colorama library for colored output in the terminal. Here’s a breakdown:

Imports:

from colorama import Fore

The code imports the Fore class from the Colorama library, which is used to set text color in the terminal.

Function Definition:

def heart_shape(msg=”Merry Christmas”):

The code defines a function named heart_shape that takes an optional parameter msg with a default value of “Merry Christmas”.

Creating the Heart Shape:

lines = []

for y in range(15, -15, -1):

    line = “”

    for x in range(-30, 30):

        f = ((x * 0.05) ** 2 + (y * 0.1) ** 2 – 1) ** 3 – (x * 0.05) ** 2 * (y * 0.1) ** 3

        line += msg[(x – y) % len(msg)] if f <= 0 else ” “

    lines.append(line)

The nested loops iterate over y-coordinates and x-coordinates to create a heart shape. The mathematical expression within the inner loop defines the shape. If f is less than or equal to 0, it adds a character from the message; otherwise, it adds a space.

Printing the Heart Shape in Red:

print(Fore.RED + “\n”.join(lines))

The heart shape is printed in red by concatenating the lines into a single string using “\n”.join(lines) and using Fore.RED from Colorama.

Printing the Message in Green:

print(Fore.GREEN + msg)

The message is printed in green using Fore.GREEN from Colorama.

Function Invocation:

heart_shape()

The function is called without passing any arguments, so it uses the default message “Merry Christmas”. The heart shape and message are printed with colored text in the terminal.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button