You can download this code by clicking the button below.
This code is now available for download.
This function creates an interactive questionnaire with a random question, choices, and separators using the Questionary library.
Technology Stack : Questionary
Code Type : Function
Code Difficulty : Intermediate
def random_questionary_choice():
import random
from questionary import Question, Choice, Separator
# Create a list of random Questionary components
components = [
Question("What is your name?", type="str"),
Choice("Choose a number", ["1", "2", "3", "4", "5"]),
Separator(),
Choice("Select an option", ["Option A", "Option B", "Option C"], default="Option B")
]
# Shuffle the components to get a random combination
random.shuffle(components)
# Construct the question based on the random components
question = Question("Please answer the following question:", type="str")
for component in components:
if isinstance(component, Choice):
question = question.add_choice(component.choices)
elif isinstance(component, Separator):
question = question.add_separator()
else:
question = question.add_question(component)
# Ask the question and return the response
return question.ask()
# Code metadata
metadata = {
"type": "Function",
"hard": "中级",
"explain": "该函数使用Questionary库创建一个包含随机问题、选择和分隔符的交互式问卷。",
"tench": "Questionary",
"explain_en": "This function creates an interactive questionnaire with a random question, choices, and separators using the Questionary library.",
"tench_en": "Questionary"
}
# Example usage of the function
response = random_questionary_choice()
print(response)