Like taking a colored test to a Python console.



  • I need the message to be green. right in the console♪ Writing code:

    def out_red(text):
        print("\033[31m {}" .format(text))
    def out_yellow(text):
        print("\033[33m {}" .format(text))
    def out_blue(text):
        print("\033[34m {}" .format(text))
    out_red("Вывод красным цветом")
    out_yellow("Текст жёлтого цвета")
    out_blue("Синий текст")
    

    But it's like,

    [31m Вывод красным цветом
    [33m Текст жёлтого цвета
    [34m Синий текст
    

    Help me, please.



  • Yay, I got the answer! Here. https://ru.stackoverflow.com/questions/1135953/%D0%A6%D0%B2%D0%B5%D1%82%D0%BD%D0%BE%D0%B9-%D1%82%D0%B5%D0%BA%D1%81%D1%82-%D0%B2-idle-%D0%BE%D0%B1%D0%BE%D0%BB%D0%BE%D1%87%D0%BA%D0%B5-%D0%BD%D0%B0-python?rq=1

    Here's the code:

    import sys
    

    def cprint(text, color):
    try:
    shell_stream = sys.stdout.shell
    except AttributeError:
    raise RuntimeError("Use IDLE")
    shell_stream.write(text, color)
    shell_stream.write('\n')

    Тесты:

    cprint('Hello, world', 'KEYWORD')
    cprint('How are you?', 'STRING')


Log in to reply
 

Suggested Topics