我正在用Python创建一个基于文本的游戏,需要有关splat参数的帮助.我有一个测试输入是否有效的功能,并且还允许您访问库存.我有两个参数,一个用于获取输入提示,另一个是对该提示的有效答案.答案是一个splat参数,因为您可以对提示有多个答案.这是该函数:
def input_checker(prompt, *answers):
user_input = raw_input(prompt).lower()
if user_input == "instructions":
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循环,再次要求输入,但是如果我删除“是”或“否”,那么只有答案列表,它将起作用
我该如何处理两个论点?我做错了什么?