简体   繁体   中英

Creating a text based game in Python. How to check for user-input?

I am creating a text based game in Python and need help with splat parameters. I have a function that tests if input is valid, and also allows you to access your inventory. I have two parameters, one that gets the input prompt, and one that is valid answers to that prompt. The answers is a splat parameter because you can have multiple answers to the prompt. Here is that function:

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 that contain common 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 perform the while loop that asks for the input again, but if I remove the 'yes' or 'no' so there is only answer list, it will work

How do I go about having two arguments? What did I do wrong?

Why about declaring the function like this:

def input_checker(prompt, answers):  

#...

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

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

I believe what you are after is the following implementation of 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" would never terminate when passing in two lists as "valid input" when you declared *answers or in my example *valid because you are trying to check if user_input or s in my case is a member of a tuple containing 2 items ( 2 lists ).

In your case answers would 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
def input_checker(prompt, *answers):  
# ...
pizza = input_checker("Do you like pizza? ", yes, no)

So answers is tuple (["yes", "y", "yup", "yep"], ["no", "n", "nope"]) .

(If you'd call input_checker("Do you like pizza? ", yes, no, ["foo", "bar"]) then answers will be (["yes", "y", "yup", "yep"], ["no", "n", "nope"], ["foo, "bar") )

And expression in loop

while user_input not in answers:

will return False and will never end. You can change code like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM