3rd course email - 'A little more Functions, please'


A few thoughts about the def get_color_and_print(shape) function from the latest email for the third mooc module.

I never thought about, or realized you could ‘print’ different colors in the command prompt. I thought it would be a fun thing to try… But this was not an easy thing for me to get it to work. Maybe it works easily on a Mac or Linux, but not Windows…

Using ‘ANSI escape codes’ for an example intro to functions, will probably confuse all but the hardiest beginners…(imho)

The code would print ‘gibberish’ for the colored text… But after a while I figured out I needed install a module, termcolor, for this to work. Still didn’t work. After reading more carefully about the Termcolor module, I realized it works only for Linux. But then found another module, colorama, which does work… And can work with the Termcolor module. Finally got it to work!

But not so fast… This worked in PowerShell but will not work in Python’s IDLE. After a lot more research, I found out it will never work because of IDLE’s GUI.

If interested, here is what I did with the code from the email but also added the imported modules:

#tetris game
from colorama import init
from termcolor import colored


shapes = ["_|", "|_|_|", "|_", "_/~", "|", "~\_"]

for shape in shapes:
    print shape

def get_color_and_print(shape):
    if shape == '_|':
        print '\033[95m' + shape + '\033[0m'
    elif shape == '|_|_|':
        print '\033[94m' + shape + '\033[0m'
for shape in shapes:

print colored('Hello', 'red'), colored('World!!', 'green')

*You need to install the modules to be able to import them for this to work… Which is not the most beginner friendly thing to figure out.
ANSI escape code
Print in terminal with colors

Even though doing this was not required… I learned a lot by doing it.

By the way, the code in the email was also missing a colon… :wink:

The Tetris Project