3

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?

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I'm kind of new to Python. What is this try statement for?
The try/except defines input for Python 2/3 compatibility.
1
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)

2 Comments

If i make the argument splat, and do the yes + no method, why doesn't it work? It will work if I take it away.
You mean if you make input_checker(prompt, *answers) with * before answer? Then all arguments after prompt go to tuple answers. Even lists. In case input_checker(prompt, yes + no) answers will be tuple with list element in it, i. e. answers is tuple (["yes", "y", "yup", "yep", "no", "n", "nope"]). In while loop you check if user_input not in answers, i. e. if any of answers elements is string user_input. But answers only element is yes + no list. It doesn't check if user_input in that list, it doesn't go deep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.