【问题标题】:Creating a text based game in Python. How to check for user-input?在 Python 中创建基于文本的游戏。如何检查用户输入?
【发布时间】:2015-07-28 19:05:34
【问题描述】:

我正在用 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 循环,但如果我删除“是”或“否”,所以只有答案列表,它会起作用

如何处理两个参数?我做错了什么?

【问题讨论】:

    标签: python while-loop user-input text-based splat


    【解决方案1】:

    为什么要这样声明函数:

    def input_checker(prompt, answers):  
    
    #...
    

    并在调用函数时将尽可能多的有效回复列表作为串联列表传递?

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

    【讨论】:

      【解决方案2】:

      我相信您所追求的是userinput() 的以下实现:

      示例:

      #!/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
      

      演示:

      >>> 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 是对的;当您声明 *answers 或在我的示例中为 *valid 时将两个列表作为“有效输入”传递时,您的“while 循环”将永远不会终止,因为您正在尝试检查 user_inputs 在我的情况下是否是tuple 的成员,包含 2 个项目(2 个列表)。

      在您的情况下,answers 看起来像这样:

      answers = (["yes", "y", "yup", "yep"], ["no", "n", "nope"],)
      

      为了说明这一点:

      >>> answers = (["yes", "y", "yup", "yep"], ["no", "n", "nope"],)
      >>> "yes" in answers
      False
      >>> "no" in answers
      False
      

      【讨论】:

      • 我对 Python 有点陌生。这个 try 语句有什么用?
      • try/except 定义了input 以实现 Python 2/3 兼容性。
      【解决方案3】:
      def input_checker(prompt, *answers):  
      # ...
      pizza = input_checker("Do you like pizza? ", yes, no)
      

      所以answers 是元组(["yes", "y", "yup", "yep"], ["no", "n", "nope"])

      (如果您打电话给input_checker("Do you like pizza? ", yes, no, ["foo", "bar"]),那么answers 将是(["yes", "y", "yup", "yep"], ["no", "n", "nope"], ["foo, "bar")

      循环中的表达式

      while user_input not in answers:
      

      将返回False 并且永远不会结束。 您可以像这样更改代码

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

      【讨论】:

      • 如果我把参数弄得一塌糊涂,然后用 yes + no 方法,为什么它不起作用?如果我把它拿走,它会起作用。
      • 你的意思是如果你在answer之前用*创建input_checker(prompt, *answers)?然后prompt 之后的所有参数都转到元组answers。甚至列表。如果input_checker(prompt, yes + no) answers 将是 tuple,其中包含 list 元素,i。 e. answers元组 (["yes", "y", "yup", "yep", "no", "n", "nope"])。在 while 循环中,您检查 user_input not in answers,i。 e.如果任何answers 元素是string user_input。但是answers 唯一的元素是yes + no 列表。它不会检查 user_input 是否在该列表中,它不会深入。
      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多