
import textwrap


def execute_code_and_get_result(code_str, exec_globals, result_key=None):
    
    
    code = textwrap.dedent(code_str)
    local_env = {}

    
    exec(code, exec_globals, local_env)

    
    return None if result_key is None else local_env.get(result_key)
