Home > Back-end >  Is it possible to use a python list as a choice?
Is it possible to use a python list as a choice?

Time:05-24

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.

x)

CodePudding user response:

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]

You could convert the input into lower- or uppercase before matching and skip the list.

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]

Or make a list of directions all lowercase and check if user_choice.lower() is in the list.

Code:


directions = ['left', 'right', 'forward', 'back']

while True:
    user_choice = input('choose direction: ')
    if user_choice.lower() in directions:
        print(user_choice.lower())
        break
    else:
        print('choice not valid please choose agaim')
        continue

Output:

choose direction: To space
choice not valid! Please choose again
choose direction: RIght
right

[Program finished]

CodePudding user response:

You should make your test set-theoretic. E.g. :

if choice in Set_of_Left_Turns or choice in Set_of_Right_Turns: print('first branch') else: print('second branch')

Also, it would simplify matters if you restrict your choices variable to just the upper-case answers and then you use choice.upper() to compare an input which has been mapped to upper-case against a set of upper-case choices.

More generally, using ideas (such as a Singular Value Decomposition), you can use an algorithm to map a textual phrase down to a few key nouns, and then make the determination based on that sufficient statistic, rather than the original text. Ie you can use dimensionality reduction ideas.

Still more generally, Fuzzy Logic is related in that you can 'defuzzify' the input, compute a function of this result, and then fuzzify the output. This notion is also related to the working of an autoencoder in a Deep Learning / neural network. These are all dimensionality reduction principles a la Principle Components Analysis.

I recommend in your RPG you make the set of actions static (or give them explicitly) such that your users know what the candidate choices are. Take a look at e.g. Zork or Ultima 5 for inspiration.

Last thought, study up on the possible emulations of a switch statement in Python:

What is the Python equivalent for a case/switch statement?

  • Related