Reputation: 63
So, I'm making a text based puzzle game using Python for my programming class (we are forced to use python), so instead of having the user say something simple like 1 or 2, I want the program to detect if the user has entered something like 'Hesitate' or 'Walk' exactly.
Currently I have it determine the amount of characters in the user's input, however this makes it possible for them to input almost anything.
#Choice Number1
def introchoice():
print("Do you 'Hesitate? or do you 'Walk forward")
def Hesitate():
print()
print("You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer. \n ''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.")
print()
#
def Walk():
print()
print("DEFAULT")
print()
#Currently Determines Input
InputVar = 5
#User Input
Choice = str(input())
#Checks length
if len(Choice) >= InputVar:
Hesitate()
else:
if len(Choice) <= InputVar:
Walk()
else:
print("Invalid Input")
#Intro Choice Def end
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#Clean Up
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
How can I change this so If the input is anything but Walk or Hesitate it wont take the input (in this current code, Walk is not included in the code yet). I want it to be something like;
if input = "Hesitate"
print("You Hesitate")
else:
if input = "Walk"
print("You walk forward")
else:
print("Invalid Input")
I can't figure out how I can properly do this in python. I honestly searched everywhere.
Upvotes: 1
Views: 7872
Reputation: 1581
As for the accepted answer, it's generally bad practice to put all of your prompt text in the input()
function. Also, don't use the len()
function, it's bad practice. What you should do is:
def introchoice():
#don't bother using three print statements every time.
def printb(texttoprint):
print()
print(texttoprint)
print()
def Hesitate():
printb('You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer. \n ''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.')
def Walk():
printb('Default')
input=null
print('Do you Hesitate or Walk')
input=input('')
#Python3 is case sensitive, and you want to make sure to accept all cases
while input.upper()!='HESITATE' and input.upper()!='WALK':
print('Invalid input')
input=input('')
This code will do what you are asking to do. Don't check your input's length in determining it's value, you should loop until you get something valid.
Upvotes: 1
Reputation: 383
You want to grab the user input and then use a while loop that redefines the user input variable until you get valid input. So something like this:
var = raw_input("Enter Text: ")
while (var != "X" and var != "Y"):
var = raw_input("Pick a different option: ")
Edit: raw_input() was changed to input() python 3.x
Upvotes: 1
Reputation: 134
Is this using the console?
while True:
input = input("What do you do")
if input == "Choice1":
print("Consequence1")
break
if input == "Choice2":
print("Consequence2")
break
if input == "Choice3":
print("Consequence3")
break
This is not the best way to go about doing this, but it easily achieves what I am interperting you are asking.
Really, the answer lies in how you do the loop.
In this simple method, it continously runs until it breaks after being given valid input, a more efficent solution could argueably be found using a make-shift switch statement with a dictionary, an example of which is discussed here. Alternatively you could just use a more specific conditional in your loop statement.
Upvotes: 3
Reputation: 1075
Based on another stackOverflow thread, I'm inclined to believe you'd want something like this:
if input == "Hesitate":
print("You Hesitate")
else:
if input == "Walk":
print("You walk forward")
else:
print("Invalid Input")
I'm assuming here that you've already gotten the input value. Also, given this answer, you would need to force capitalization to a predetermined form (first letter capital all remaining lower). This thread may be helpful for that.
Finally, it might be helpful to review the difference between assignment and equality. In several common programming languages, you would use '=' to assign a value to a variable. Where as in most of those same languages, you would use '==' to determine the equality of variables or constants.
Upvotes: 2