Writing a text game in Python. How to check user input?

I am creating a text game in Python and need help with splat parameters. I have a function that validates the input and also allows you to access your inventory. I have two parameters, one that receives an input prompt, and one that is a valid response to that prompt. Replies is a splat parameter because you can have multiple responses per prompt. This function is:

def input_checker(prompt, *answers):  

    user_input = raw_input(prompt).lower()

    if user_input == "instructions":
        print
        instructions()

    elif user_input == "i" or user_input == "inventory":
        if len(inventory) == 0:
            print "There is nothing in your inventory."
        else:
            print "Your inventory contains:",  ", ".join(inventory)

    else:
        if user_input in answers:
        return user_input
        else:
            while user_input not in answers:
                user_input = raw_input("I'm sorry.  I did not understand your answer.  Consider rephrasing. " + prompt )
                if user_input in answers:
                    return user_input
                    break

      

I have two lists containing general answers to questions:

yes = ["yes", "y", "yup", "yep"]  
no = ["no", "n", "nope"]

      

If I call the function like this:

pizza = input_checker("Do you like pizza? ", yes, no)

      

It will always do a while loop that asks for input again, but if I remove the yes or no, so there is only a list of answers, it works

How do I get two arguments? What have I done wrong?

+3


source to share


3 answers


Why about declaring a function like this:

def input_checker(prompt, answers):  

#...

      



And pass as many lists of valid responses as possible as a concatenated list when you call the function?

pizza = input_checker("Do you like pizza? ", yes + no)

      

+1


source


I believe you are following the following implementation userinput()

:

Example:

#!/usr/bin/env python

try:
    input = raw_input
except NameError:
    pass

def userinput(prompt, *valid):
    s = input(prompt).strip().lower()
    while s not in valid:
        s = input(prompt).strip().lower()
    return s

      

Demo:

>>> userinput("Enter [y]es or [n]o: ", "y", "n")
Enter [y]es or [n]o: a
Enter [y]es or [n]o: foo
Enter [y]es or [n]o: y
'y'

      



@Jorge Torres is right; Your "while loop" will never terminate when passed in two lists as "valid input" when you declared *answers

or in my example *valid

, because you are trying to check that user_input

or s

in my case is a member tuple

containing 2 elements (2 lists).

In your case it answers

will look like this:

answers = (["yes", "y", "yup", "yep"], ["no", "n", "nope"],)

      

To illustrate this point:

>>> answers = (["yes", "y", "yup", "yep"], ["no", "n", "nope"],)
>>> "yes" in answers
False
>>> "no" in answers
False

      

+1


source


def input_checker(prompt, *answers):  
# ...
pizza = input_checker("Do you like pizza? ", yes, no)

      

So, answers

is a tuple (["yes", "y", "yup", "yep"], ["no", "n", "nope"])

.

(If you choose input_checker("Do you like pizza? ", yes, no, ["foo", "bar"])

then there answers

will be (["yes", "y", "yup", "yep"], ["no", "n", "nope"], ["foo, "bar")

)

And the expression in the loop

while user_input not in answers:

      

will return False

and never end. You can change the code like this

input_checker(prompt, answers):
# ...
pizza = input_checker("Do you like pizza? ", yes + no)

      

+1


source







All Articles