Skip to content

Python choice input | Example code

  • by

You have to use while true with if statement for multiple-choice input in Python. Multiple choice needed multiple if-else statements.

Python choice input example

Simple example code. “Do you want to: A) Approach the house. B) Approach the stable. [A/B]?” and stop when you enter Q. It would be up to you to continue this structure of code with more logic to suit your desired implementation.

while True:

    d1a = input("Do you want to: \n A) Approach the house. B) Approach the stable. [A/B]? : ")

    if d1a == "A":
        print("You approach the cottage.")
    elif d1a == "B":
        print("You approach the stables.")
    elif d1a == "Q":


Python choice input

Note: Writing in this style will get difficult and complex. So split up code into functions, modules, etc.


Another example

def get_user_choice():
    options = {
        '1': 'Option 1',
        '2': 'Option 2',
        '3': 'Option 3',
        '4': 'Option 4',

    while True:
        print("Select an option:")
        for key, value in options.items():
            print(f"{key}: {value}")

        user_input = input("Enter the number of your choice: ")

        if user_input in options:
            return options[user_input]
            print("Invalid choice. Please try again.")

# Example usage
user_choice = get_user_choice()
print(f"You selected: {user_choice}")

n this example, the get_user_choice() function presents the user with a list of options. The user is asked to enter the corresponding number of their choice. The function will keep asking until the user provides a valid input (a number corresponding to one of the options). Once a valid choice is made, the function returns the chosen option.

Keep in mind that this is a basic example, and you can modify it according to your specific use case and the number of options you have. Additionally, you may want to add error handling to handle unexpected user inputs gracefully.

Comment if you have any doubts or suggestions on this Python input program.

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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading