PyInquirer Survey Creation with Random Questions

  • Share this:

Code introduction


This function uses the PyInquirer library to create a survey with multiple questions, where users can input their answers through the command line.


Technology Stack : PyInquirer

Code Type : Survey

Code Difficulty : Intermediate


                
                    
import random
from pyinquirer import prompt, Survey, Choice, Input, List, SecretInput

def random_survey():
    questions = [
        Choice('question1', choices=['What is your name?', 'What is your favorite color?']),
        Input('question2', message='Enter your age:'),
        List('question3', message='Select your favorite fruit:', choices=['Apple', 'Banana', 'Cherry']),
        SecretInput('question4', message='Enter your password:')
    ]

    survey = Survey(questions)
    answers = survey.collect Responses()
    return answers                
              
Tags: