Random Feature Question Generator Using PyInquirer

  • Share this:

Code introduction


Randomly selects a feature from the PyInquirer library and generates a custom function to ask the user a specific question.


Technology Stack : Randomly selects a feature from the PyInquirer library and generates a custom function to ask the user a specific question.

Code Type : Custom function

Code Difficulty :


                
                    
import random
from pyinquirer import prompt, ChoiceQuestion, InputQuestion

def select_random_feature():
    features = [
        {
            "question": "What is your name?",
            "type": "InputQuestion",
            "hard": "初学",
            "explain": "询问用户的名字。",
            "tench": "PyInquirer",
            "explain_en": "Ask the user for their name.",
            "tench_en": "PyInquirer"
        },
        {
            "question": "What is your favorite color?",
            "type": "ChoiceQuestion",
            "hard": "初学",
            "explain": "询问用户最喜欢的颜色,并从给定选项中选择。",
            "tench": "PyInquirer",
            "explain_en": "Ask the user for their favorite color and select from the given options.",
            "tench_en": "PyInquirer"
        },
        {
            "question": "Rate your happiness today on a scale from 1 to 10?",
            "type": "InputQuestion",
            "hard": "中级",
            "explain": "询问用户今天的幸福感,并接收一个1到10之间的数字。",
            "tench": "PyInquirer",
            "explain_en": "Ask the user to rate their happiness today on a scale from 1 to 10.",
            "tench_en": "PyInquirer"
        },
        {
            "question": "Which programming language do you use?",
            "type": "ChoiceQuestion",
            "hard": "中级",
            "explain": "询问用户使用的编程语言,并从给定选项中选择。",
            "tench": "PyInquirer",
            "explain_en": "Ask the user which programming language they use and select from the given options.",
            "tench_en": "PyInquirer"
        },
        {
            "question": "What is the capital of France?",
            "type": "InputQuestion",
            "hard": "高级",
            "explain": "询问法国的首都是什么,并接收用户的回答。",
            "tench": "PyInquirer",
            "explain_en": "Ask what the capital of France is and receive the user's answer.",
            "tench_en": "PyInquirer"
        }
    ]
    
    selected_feature = random.choice(features)
    return selected_feature

def generate_question_function(feature):
    question_data = feature
    question_type = question_data["type"]
    question = question_data["question"]
    
    if question_type == "InputQuestion":
        def xxx():
            return prompt(InputQuestion(question))
    elif question_type == "ChoiceQuestion":
        choices = ["Option 1", "Option 2", "Option 3"]  # This should be replaced with actual choices
        def xxx():
            return prompt(ChoiceQuestion(question, choices=choices))
    
    return xxx

selected_feature = select_random_feature()
generate_function = generate_question_function(selected_feature)

# Example usage:
# answer = generate_function()
# print(answer)