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":
        print("Nothing.")
        break

Output:

Python choice input

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

Source: stackoverflow.com

Do comment if you have any doubts and suggestions on this Python input program.

Note: IDE: PyCharm 2021.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

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.