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?
                           ^^^^^^^^^^
6
  • 1
    You need quotes around the RHS Commented Dec 17, 2022 at 9:58
  • What exactly is the RHS? Commented Dec 17, 2022 at 10:00
  • 1
    The right-hand side of an assignment expression. Commented Dec 17, 2022 at 10:01
  • 1
    As an aside, you almost certainly want to use dictionaries instead of global variables with computed values. See also stackoverflow.com/questions/1373164/… Commented Dec 17, 2022 at 10:19
  • What is the desired behaviour of calling exec(bigstring) here exactly? Commented Dec 17, 2022 at 10:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.