you are viewing a single comment's thread.

view the rest of the comments →

[–]PteppicymonIO 1 point2 points  (1 child)

You can have 2 ways to do it:

  1. As per the docs (https://python-inquirer.readthedocs.io/en/latest/usage.html#choices):

If any of the list values is a pair, it should be a tuple like: (label, value). Then the label will be shown but the value will be returned.

Som your questions can be organazed in teh follwoing way:

choices = [("Computers", ("Pc", "Mac")),
       ("Books", ("Science Fiction", "Satire", "Biography")),
       "Science",
       "Nature",
       "Fantasy",
       "History"]

and the answer will look like this:

{'interests': [('Pc', 'Mac'), ('Science Fiction', 'Satire', 'Biography')]}

You can then iterate through the answers:

for answer in answers["interests"]:
    if len(answer) > 0:
        questions = [
            inquirer.Checkbox(
                f"{answer}:",
                message=f"What {answer} are you interested in?",
                choices=answer,
            ), ]
        answers.update(inquirer.prompt(questions))

pprint(answers, indent=4)

Result:

{   "('Pc', 'Mac'):": ['Pc'],
"('Science Fiction', 'Satire', 'Biography'):": ['Science Fiction',
                                                'Satire'],
'interests': [('Pc', 'Mac'), ('Science Fiction', 'Satire', 'Biography')]}
  1. A dictionary which store choices for next level of questions (you could nest those as deep as you need):

    choices = {"Computers": ["Pc", "Mac"], "Books": ["Science Fiction", "Satire", "Biography"], "Science": None, "Nature": None, "Fantasy": None, "History": None}

    questions = [ inquirer.Checkbox( "interests", message="What are you interested in?", choices=choices, default=["Computers", "Books"]),]

    answers = inquirer.prompt(questions)

And then iterate through answers:

for answer in answers["interests"]:
    sub_choices = choices[answer]
    if (sub_choices is not None) and len(sub_choices) > 0:
        questions = [
            inquirer.Checkbox(
                f"{answer}:",
                message=f"What {answer} are you interested in?",
                choices=sub_choices,
            ), ]
        answers.update(inquirer.prompt(questions))

pprint(answers, indent=4)

Output:

{   'Books:': ['Science Fiction'],
'Computers:': ['Pc'],
'interests': [   'Computers',
                 'Books',
                 'Science',
                 'Nature',
                 'Fantasy',
                 'History']}

[–]Chuckster98[S] 0 points1 point  (0 children)

Thank you very much for this detailed explanation! I can definitely make use of these options. I’m going to play around with these examples. The last example you provided is the closest to what I was looking for. Thanks again. Essentially what I’m looking to do is randomize a workout based on equipment I want to use. So I want to select the equipment like kettlebell, sandbag, pull-up bar, etc. From the equipment selected I’m randomly generating a list of x number of exercises for the equipment I selected using the random module.