我正在用 Python 创建一个基于文本的游戏,需要有关 splat 参数的帮助。我有一个功能可以测试输入是否有效,还允许您访问您的库存。我有两个参数,一个获取输入提示,一个是该提示的有效答案。答案是一个 splat 参数,因为您可以对提示有多个答案。这是该功能:
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
我有两个包含常见问题答案的列表:
yes = ["yes", "y", "yup", "yep"]
no = ["no", "n", "nope"]
如果我这样调用函数:
pizza = input_checker("Do you like pizza? ", yes, no)
它总是会执行再次要求输入的while循环,但如果我删除“是”或“否”,所以只有答案列表,它会起作用
我该如何处理两个论点?我做错了什么?