Saturday 23 December 2023

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

#clcoding.com


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.

Note: The comment #clcoding.com at the end is a comment and doesn't affect the code's functionality. It seems to be a reference to a website.



0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (115) C (77) C# (12) C++ (82) Course (62) Coursera (179) coursewra (1) Cybersecurity (22) data management (11) Data Science (91) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (747) Python Coding Challenge (208) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses