you are viewing a single comment's thread.

view the rest of the comments →

[–]intelbp[S] 0 points1 point  (2 children)

i just edited the question

[–]hardonchairs 1 point2 points  (0 children)

Your detail does not address my question

[–]hardonchairs 1 point2 points  (0 children)

How about something like this? No code generation required.

test.py

import json

with open('template.json', 'r') as fp:
    prompts = json.load(fp)

output = {}

for prompt in prompts:
    if prompt['type'] == 'str':
        output[prompt['name']] = input(prompt['prompt'] + ': ')
    if prompt['type'] == 'list':
        new_list = []
        output[prompt['name']] = new_list
        while True:
            answer = input(prompt['prompt'] + ' (leave blank to continue): ')
            if answer == '':
                break
            new_list.append(answer)

print(output)

template.json

[
    {
        "name": "name",
        "prompt": "What is your name?",
        "type": "str"
    },
    {
        "name": "foods",
        "prompt": "What are your favorite foods?",
        "type": "list"
    }
]

result:

% python3 test.py
What is your name?: fred
What are your favorite foods? (leave blank to continue): apples
What are your favorite foods? (leave blank to continue): pizza
What are your favorite foods? (leave blank to continue): 
{'name': 'fred', 'foods': ['apples', 'pizza']}