<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_theme(theme=None, for_code=None):
 """ set md and code theme """

    try:
        if theme == 'default':
            return
        theme = theme or os.environ.get('AXC_THEME', 'random')
        # all the themes from here:
        themes = read_themes()
        if theme == 'random':
            rand = randint(0, len(themes)-1)
            theme = themes.keys()[rand]
        t = themes.get(theme)
        if not t or len(t.get('ct')) != 5:
            # leave defaults:
            return
        _for = ''
        if for_code:
            _for = ' (code)'
        if is_app:
            print >> sys.stderr, low('theme%s: %s (%s)' % (_for, theme,
                                                       t.get('name')))
        t = t['ct']
        cols = (t[0], t[1], t[2], t[3], t[4])
        if for_code:
            global CH1, CH2, CH3, CH4, CH5
            CH1, CH2, CH3, CH4, CH5 = cols
        else:
            global H1, H2, H3, H4, H5
            # set the colors now from the ansi codes in the theme:
            H1, H2, H3, H4, H5 = cols
    finally:
        if for_code:
            build_hl_by_token()