python创建提示用户输入_用Python创建一个基于文本的游戏。如何检查用户输入?...

我正在用Python创建一个基于文本的游戏,需要splat参数的帮助。我有一个测试输入是否有效的函数,还允许您访问您的库存。我有两个参数,一个获取输入提示,另一个是该提示的有效答案。answers是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

我有两个列表,其中包含问题的常见答案:

^{pr2}$

如果我像这样调用函数:pizza = input_checker("Do you like pizza? ", yes, no)

它将始终执行while循环,再次请求输入,但如果我删除“yes”或“no”以便只有答案列表,它就可以工作了

我怎么能有两个论点呢?我做错什么了?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值