print green digital rain in pure python in python

To print green digital rain in pure Python, you can use the following code:

main.py
import random
import sys
import time

# Define a list of characters to use in the digital rain
chars = ['0', '1']

# Define a function to generate a random digit
def rand_digit():
    return random.choice(chars)

# Define a function to generate a random line of digital rain
def rand_line(width):
    return ''.join([rand_digit() for _ in range(width)])

# Define function for printing a line of digital rain to the console
def print_line(line):
    sys.stdout.write("\033[32m%s\033[0m" % line)  # set text color to green
    sys.stdout.flush()
    time.sleep(0.05)

# Define a function to print a random digital rain pattern
def print_digital_rain(width, height):
    for i in range(height):
        line = rand_line(width)
        print_line(line)
        
# Example usage
print_digital_rain(100, 50)
803 chars
30 lines

In this code, we use ANSI escape codes to set the text color to green before printing each line of digital rain. The sys.stdout.flush() function is used to force the console to immediately print each line as it is generated, rather than waiting until the end of the loop. Finally, the time.sleep(0.05) function is used to add a small delay between each line of digital rain, creating an animation effect.

gistlibby LogSnag