0

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?
                           ^^^^^^^^^^
BrianBeing
  • 431
  • 2
  • 4
  • 12

1 Answers1

-1

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.".

Ivan
  • 17
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '22 at 01:09