'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.
x)
Solution 1:[1]
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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Darin |
