Skip to content

How to make a multiple-choice question in Python | Example code

Using while true with if statement you can make a multiple-choice question in Python. stop the loop if the user enters “Q”.

To create a multiple-choice question in Python, you can use a function to display the question and its choices, and then check the user’s input against the correct answer. Here’s the Python syntax for creating a multiple-choice question:

def multiple_choice_question(question, choices, correct_answer):
    """
    Function to display a multiple-choice question, its choices, and validate the user's answer.

    Parameters:
        question (str): The question text.
        choices (list of str): A list containing the multiple-choice options.
        correct_answer (int): The index (0-based) of the correct answer in the choices list.

    Returns:
        bool: True if the user's answer is correct, False otherwise.
    """
    print(question)
    for idx, choice in enumerate(choices):
        print(f"{idx + 1}. {choice}")

    user_answer = int(input("Enter the number corresponding to your answer: ")) - 1

    return user_answer == correct_answer


# Example usage:
question_text = "What is the capital of France?"
choices_list = ["London", "Paris", "Berlin", "Rome"]
correct_answer_index = 1  # The correct answer index in the choices_list (in this case, 1 corresponds to "Paris")

is_correct = multiple_choice_question(question_text, choices_list, correct_answer_index)

if is_correct:
    print("Congratulations! Your answer is correct.")
else:
    print("Oops! Your answer is incorrect.")

Example make a multiple-choice question in Python

Simple example code making a very simple multiple-choice story in Python, repeat if neither of the options is selected.

while True:
    d1a = input("Do you want to: \n A) House. B) Stable. [A/B]? : ")
    if d1a == "A":
        print("You approach the cottage.")
    elif d1a == "B":
        print("You approach the stables.")
    elif d1a == "Q":
        print("Done!")
        break

Output:

How to make a multiple-choice question in Python

Another example

def multiple_choice_question(question, choices, correct_answer):
    print(question)
    for idx, choice in enumerate(choices, start=1):
        print(f"{idx}. {choice}")

    user_answer = int(input("Enter the number corresponding to your answer: "))

    if user_answer == correct_answer:
        print("Correct!")
    else:
        print("Incorrect. Try again!")

# Example usage:
question_text = "What is the capital of France?"
choices_list = ["London", "Paris", "Berlin", "Rome"]
correct_answer_index = 2  # The correct answer index in the choices_list (in this case, 2 corresponds to "Paris")

multiple_choice_question(question_text, choices_list, correct_answer_index)

In this example, the multiple_choice_question function takes the question, a list of choices, and the index of the correct answer as input. It then prints the question and the choices, takes user input for the answer, and compares it to the correct answer.

You can customize this code further to add more questions, create a loop to ask multiple questions and keep track of the user’s score, among other things.

Do comment if you have any doubts or suggestions on this Python program code.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

2 thoughts on “How to make a multiple-choice question in Python | Example code”

Leave a Reply

Your email address will not be published. Required fields are marked *