【问题标题】:Making a questionnaire that depends on user's previous answers根据用户之前的回答制作问卷
【发布时间】:2019-12-16 15:01:52
【问题描述】:

我最近开始编写一个 Python 程序,我想在该程序中“引导”用户完成答案。我认为最好的表达方式是举个例子:

假设我有一份问卷,其中有 2 个问题,但只有在用户回答了之前的问题时才能询问这些问题:

  • Q1:您是否年满 18 岁?
  • A1:是(重定向到问题二
  • A1:否(*重定向到说明用户无法参与调查问卷的文字)

我也在尝试用它做更多的事情。我的完整计划是:

有一个初始问题,询问用户想要使用三个功能中的哪一个。

  • 第一个功能:检查用户是否可以申请(年龄、状态等)
  • 第二个功能:列出申请的分步
  • 第三个功能:提供问答列表。

我写到现在的代码只针对第一个函数,有很多错误(我以前从来没有写过代码)。

  • 如何格式化此编码以使其适合我想要做的整个方案?
  • 如何做到这一点,以便用户在输入错误(不是输入 y 或 n)时不会崩溃,而是循环回问题?

到目前为止我的代码:

print ("Answer all yes or no questions with Y or N")
while True:
  idade = input("Are you over 18? Y/N")
  if idade.lower() not in ('y', 'n'):
    print("Answer only with Y or N")
  else:
    if idade == "Y" or idade == "y":
      crime = input("Have you ever been arrested or convicted before?")
    if idade == "N" or idade == "n":
      print("Sorry, you can't apply. ")
  if crime.lower() not in ('y', 'n'):
      print("Answer only with Y or N")
  else:
     if crime == "Y" or crime == "y":
      print("Sorry, you can't apply. ") 
     if crime == "N" or crime == "n":
      visto = input("Do you have visa TYPE_A'? ")
  if visto.lower() not in ('y', 'n'):
    print("Answer only with Y or N")
  else:
        if visto == "Y" or visto == "y":
          print("THAT'S AS FAR AS I'VE GONE")
        if visto == "N" or visto == "n":
          print("THAT'S AS FAR AS I'VE GONE")
          break

【问题讨论】:

    标签: python python-3.x question-answering


    【解决方案1】:
    def valid_input(message):
      inp = input(message)
      while inp.lower() not in ('y','n'):
        inp = input('Please input "y" or "n" only. ' + message)
      return inp.lower() == 'y' # So the return value is simply True or False
    
    
    print ("Answer all yes or no questions with Y or N.")
    while True:
      idade = valid_input("Are you over 18? Y/N: ")
      if not idade:
        print('Sorry, you can\'t apply.')
        break
      crime = valid_input("Have you ever been arrested or convicted before? Y/N: ")
      if crime:
          print("Sorry, you can't apply. ") 
          break
      else:
        visto = valid_input("Do you have visa TYPE_A'? ")
        if visto:
          print("THAT'S AS FAR AS I'VE GONE")
          break
        else:
          print('You don\'t have that visa type.')
          break
    

    要“记住”以前的值,只需重用变量名即可。 示例:

    question = valid_input('Question: ')
    another_question = valid_input('Another: ')
    if question:
        # Do stuff
        if another_question:
            # Do other stuff
    

    【讨论】:

    • 这使它更有条理,但我可以回到问卷中进一步的问题吗?例如,假设 - 当我问她问题 6 或其他问题时,我将能够(在程序内)知道该人在问题一中回答了 y。
    • 我想我可以回到 visto/crime 等中的输入。
    【解决方案2】:

    如果我是你,我会设置一个函数。

    def q(question):
        answer = input(question).lower()
        if 'y' is answer:
            return True
        elif 'n' is answer:
            return False
        else
            print('invalid input')
            return q(question)
    

    您的代码可以随时调用此函数,并返回真或假。如果用户输入“y”则为真,如果输入“n”则为假。如果两者都没有输入,则再次调用该函数。然后可以像这样调用该函数:

    if q('Are you 18? (N/Y)'):
        if q("Have you ever been arrested or convicted before?"):
            #and so on
    if q()#input question in here if the user isn't over 18 
    

    然后您可以像上面那样“嵌套”您的问题。

    【讨论】:

    • 这种嵌套问题会非常有用,感谢您的帮助。
    【解决方案3】:

    我想这可能有点过头了,但我认为这是一个很好的使用字典来创建一个相互关联的问题系统。这个解决方案的好处是,如果您需要重复一个问题或类似的问题,您不必输入两次相同的内容。它也很容易扩展,而且看起来很干净。

    字典questions 使用问题名称作为其键。第一个问题始终是"START",但您可以根据需要更改它。每个问题都有自己的字典,让您可以打印文本、提示、可能的操作,还可以决定选择某个答案时会发生什么。

    • "text" 键用于告诉用户一些不是问题的内容,并首先执行。您可以使用"text""prompt",尽管这可能不是必需的。
    • "prompt" 就是问题所在。
    • "y""n" 是您在选择该答案时要转到的问题的名称。

    • "action" 用于告诉程序在提出问题时是否有需要做的事情。我执行的唯一操作是结束问卷。

    要运行程序,请调用exec_questions(questions),其中questions 是您的字典。

    questions = {
        'START':
            {
                'prompt':'Are you 18 or over?',
                'y':'q2',
                'n':'fail'
            },
        'q2':
            {
                'prompt':'Have you ever been arrested or convicted?',
                'y':'fail',
                'n':'q3'
            },
        'q3':
            {
                'prompt':'Do you have a visa TYPE_A?',
                'y':'q4',
                'n':'fail'
            },
        'q4':
            {
                'text':'THAT\'S AS FAR AS I\'VE GONE',
                'action':'end'
            },
        'fail':
            {
                'text':'Sorry, you can\'t apply',
                'action':'end'
            }
    }
    
    def exec_questions(questions):
        print("Please answer all questions using y/n")
    
        this_question = questions['START']
        while True:
            if 'text' in this_question.keys():
                print(this_question['text'])
    
            if 'action' in this_question.keys():
                if this_question['action'] == 'end':
                    return
    
            if 'prompt' in this_question.keys():
                while True:
                    res = input(this_question['prompt']+"\n > ").lower()
                    if not res.lower() in ('y','n'):
                        print("Please type y/n")
                        continue
                    break
    
                this_question = questions[this_question[res]]
    
    exec_questions(questions)  
    

    【讨论】:

    • 感谢您的解决方案,我认为这会很好。如果我遇到任何其他问题,我一定会在这里发布。
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多