-3

I'm new to programming and this is the first roadblock that I've stumbled upon. I saw this piece of code in a TikTok video. I'm confused; I didn't know you can have colorful outputs in VS Code! Do I have to import a file or something to be able to use the colors? I don't really understand what the back.Red or the Fore.YELLOW functions mean, it would be appreciated if someone could explain. Thank you!

colours = [
Back.RED, Back.GREEN,
Back.CYAN, Back.MAGENTA
      ]
yel = Fore.YELLOW 
emojis = ['', '', '']

while True :
    for n in emojis:
        for back in colours:
            print(back+yel, end ='')
            print('  coding!', n )
            sleep(0.5)
rioV8
  • 24,506
  • 3
  • 32
  • 49

1 Answers1

1

The Summary

This piece of code is using an external library called Colorama. You need to import this library before using it. More information about colored outputs in Python can be found in this question.

The Fore and Back are constants/functions supplied by Colorama, which when you surround a string with them and print it to the terminal, give the string a colored FOREground or BACKground.

The Details

colours = [
    Back.RED, Back.GREEN,
    Back.CYAN, Back.MAGENTA
]
yel = Fore.YELLOW 
emojis = ['', '', '']

This creates a list containing background color constants provided by Colorama, sets a variable containing the yellow foreground color constant, and creates a list of emoji strings.

while True :
    for n in emojis:
        for back in colours:
            print(back+yel, end ='')
            print('  coding!', n )
            sleep(0.5)

This loops forever inside a while True, and each time, iterates through the emoji list. And for each iteration of the emoji list, iterates through the background constant list. And for each constant, it prints a string surrounded by a background color constant, the yellow foreground color constant, the word " coding!", and the emoji. Then it pauses half a second.

Nat Riddle
  • 928
  • 1
  • 10
  • 24