Support Forums

Full Version: [Challenge] Colours
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The challenge here is to create a script in Ruby (or any language you like) That will output
the string "Hello World" In several different colours.

The output should look something like:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!


Post your code and explain how it works.
Well in python there is a module called termcolor That allows you to do this. It is has a very simple function that takes two arguments, the string and the desired colour. Besides it's other functions, it lets you print in grey, red, green, yellow, blue, magenta, cyan, white.
Here is my script.
Code:
from termcolor import *

print colored('Hello World\n', 'green')
print colored('Hello World\n', 'red')
print colored('Hello World\n', 'cyan')
print colored('Hello World\n', 'magenta')
Of course this varies for different operating systems, to know if yours will print coloured text click the link above.
(02-24-2010, 12:52 PM)uber1337 Wrote: [ -> ]Well in python there is a module called termcolor That allows you to do this.

I dare you to do this without a library. =)
Lol FINE!
Code:
"""
Grey : 30
Red : 31,
Green : 32,
Yellow : 33,
Blue : 34,
Magenta : 35,
Cyan : 36,
White : 37
"""
print "\033[30mHello World!\033[0m"
print "\033[31mHello World!\033[0m"
print "\033[32mHello World!\033[0m"
print "\033[33mHello World!\033[0m"
print "\033[34mHello World!\033[0m"
print "\033[35mHello World!\033[0m"
print "\033[36mHello World!\033[0m"
print "\033[37mHello World!\033[0m"
Its just ANSI escape sequences, not that hard.
Very nice, I'll post my solution later tonight Big Grin