using exec how to save variable equal to a string

170 Views Asked by At

I am using exec to save a variable equal to a string. I am getting a SyntaxError. I'm assuming exec is getting confused with the value as string. Is this assumption accurate? Would appreciate the learnings! If I changed each question to an str(int), the code will work. Any help is much appreciated.

json_template = {
    "Introduction" : {
        "What is your Department?" : "",
        "What is your Name?" : "",
        "What is your Email?" : ""
    },
    "Context" : {
        "What is the necessary action or change?": "",
        "What is the urgency?" : "",
        "What lead to this change?" : "",
        "What is the opportunity or pain point this action solves?" : ""
    }
}

for each_category in json_template:
    for index, each_question in enumerate(json_template[each_category]):
        left_side = each_category + str(index)
        right_side = each_question

        bigstring = '='.join([left_side, right_side])

        exec(bigstring)
        print(bigstring)

Error below:

exec(bigstring)   
File "<string>", line 1
Introduction0=What is your Department?
                           ^^^^^^^^^^
1

There are 1 best solutions below

1
On

I am not quite sure, what you are trying to achieve, but I do not think the problem is in string: As you can see from link below: exec info

"The exec() method executes a dynamically created program, which is either a string or a code object.".