print random character and random colors in pure in python

We can print random characters and random colors using Python's built-in random module and the colorama library. Here is an example code snippet:

main.py
import random
from colorama import Fore, Back, Style

# define a list of characters
chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

# define a list of color codes
color_codes = [Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Back.RED, Back.GREEN, Back.YELLOW, Back.BLUE, Back.MAGENTA, Back.CYAN]

# randomly select a character and a color code
selected_char = random.choice(chars)
selected_color = random.choice(color_codes)

# print the selected character in the selected color
print(selected_color + selected_char + Style.RESET_ALL)
556 chars
16 lines

In the above code, we first define a list of characters and a list of color codes using the colorama library. We then use the random.choice() function to randomly select a character and a color code from the respective lists. Finally, we print the selected character in the selected color using the print() function. Note that we also use Style.RESET_ALL to reset the console to its default color after printing the character, so that the next print statement will not be affected by the color of the previous one.

gistlibby LogSnag