Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use a python list as a choice?

All. I am a bit new to Python. I have recently taken a course and followed a few tutorials. I am trying to explore on my own and just make "something". This is for a texted-based RPG. I am trying to read the users input from a pre-existing list I already created in case they type the choices a different way.

right_Choices = ["right", "Right", "RIGHT"]
left_Choices = ["left", "Left", "LEFT"]

Later in the code I ask the user which direction they would like to go in "left or right?" After their input I am trying to have the code understand any of the possible inputs i.e if they input "left" first then later on input "LEFT" or "Left" it will still continue to understand.

    # Check user input for left or right
    left_or_right = input("You wake up dazed in a random alley... Unsure of where you are, which way will you go? Left or Right? ")

    #Left option
    if left_or_right == left_Choices:
        ans = input("You stumble into a saloon nearly empty but some faces are there. Feel like having a drink at the bar? Or sitting at an empty table? (bar/table)? ")

        if ans == "bar":
            print("You sat at the bar and the bartender slides a glass across On the house partner! Nice! A free drink to help out. (+10HP)")
            health +=10
        elif ans == "table":
            print("You sit at an empty table and a group on men approach and seat themselves So tough guy where's our 450Gold!?")

        else:
            print("You were too drunk to walk, fell down and passed out...")

I hope my explanation is at least a bit understandable because I am pretty new to this and I am not sure how to word this question.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x)

>Solution :

Could achieve this in different ways. I assume you want a while loop though.

One way would be using if/elif against a list.

Code:

right_Choices = ["right", "Right", "RIGHT"]
left_Choices = ["left", "Left", "LEFT"]


while True:
    user_choice = input('choose direction: ')
    if user_choice in right_Choices:
        print('right')
        break
    elif user_choice in left_Choices:
        print('left')
        break
    else:
        print('choice not valid! Please choose again')
        continue

Output:

choose direction: no
choice not valid! Please choose again
choose direction: left
left

[Program finished]

Or you could convert the input into lower- or uppercase before matching.
Like this:

while True:
    user_choice = input('choose direction: ')
    if user_choice.lower() == "right":
        print('right')
        break
    elif user_choice.lower() == "left":
        print('left')
        break
    else:
        print('choice not valid please choose agaim')
        continue

Output:

choose direction: LEFT
left

[Program finished]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

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

Continue reading