Creating a Multiple-Choice Question with Questionary Library

  • Share this:

Code introduction


This function uses the Questionary library to create a simple multiple-choice question where the user can select an option as an answer.


Technology Stack : Questionary

Code Type : Interactive code generation

Code Difficulty : Intermediate


                
                    
import random
from questionary import Question, Choice, Separator, prompt

def random_choice_question():
    def xxx(question_text, choices_list):
        question = Question(question_text, choices=choices_list).ask()
        return question

    choices_list = [
        Choice("A", "Apple"),
        Choice("B", "Banana"),
        Choice("C", "Cherry"),
        Choice("D", "Date")
    ]
    
    result = xxx("What's your favorite fruit?", choices_list)
    return result                
              
Tags: