Random Questionary Generator

  • Share this:

Code introduction


This function uses the Questionary library to generate an input question based on a randomly selected type. It can be a text input, number input, select input, confirm input, or separator.


Technology Stack : Questionary

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import random
from questionary import Questionary, Choice, Separator

def random_questionary_function():
    # Define the function that generates a random questionary based on the Questionary library
    def generate_questionary():
        # Randomly choose a type of questionary
        questionary_type = random.choice(['text', 'number', 'select', 'confirm', 'separator'])
        
        # Initialize the questionary object based on the chosen type
        q = Questionary()
        
        if questionary_type == 'text':
            # Generate a text input questionary
            q.add('Enter your name:', Choice(text=''))
        elif questionary_type == 'number':
            # Generate a number input questionary
            q.add('Enter your age:', Choice(number=0))
        elif questionary_type == 'select':
            # Generate a select input questionary
            q.add('Choose your favorite color:', Choice(select=['red', 'green', 'blue']))
        elif questionary_type == 'confirm':
            # Generate a confirm input questionary
            q.add('Do you like Python?', Choice(confirm=True))
        elif questionary_type == 'separator':
            # Generate a separator questionary
            q.add(Separator())
        
        # Render the questionary
        return q.render()

    # Call the function to generate the random questionary
    return generate_questionary()

# Return the function and the associated information
random_questionary_function                
              
Tags: