【问题标题】:While loop to check for valid user input? [duplicate]While循环检查有效的用户输入? [复制]
【发布时间】:2015-07-27 20:17:30
【问题描述】:

这里的 Python 新手很抱歉,我确定这是一个愚蠢的问题,但我似乎无法在教程中解决以下挑战,该教程要求我使用 while 循环来检查有效的用户输入。

(使用Python2.7)

这是我的代码,但它不能正常工作:

choice = raw_input('Enjoying the course? (y/n)')
student_surveyPromptOn = True
while student_surveyPromptOn:
    if choice != raw_input('Enjoying the course? (y/n)'):
        print("Sorry, I didn't catch that. Enter again: ")
    else:
        student_surveyPromptOn = False 

上面的内容打印到控制台:

Enjoying the course? (y/n) y
Enjoying the course? (y/n) n
Sorry, I didn't catch that. Enter again: 
Enjoying the course? (y/n) x
Sorry, I didn't catch that. Enter again: 
Enjoying the course? (y/n)  

这显然是不正确的——当用户输入“y”或“n”时,循环应该结束,但我不知道该怎么做。我在这里做错了什么?

注意:挑战要求我同时使用 != 运算符和 loop_condition

【问题讨论】:

    标签: python while-loop user-input


    【解决方案1】:

    你可以使用条件

    while choice not in ('y', 'n'):
        choice = raw_input('Enjoying the course? (y/n)')
        if not choice:
            print("Sorry, I didn't catch that. Enter again: ")
    

    【讨论】:

      【解决方案2】:

      更短的解决方案

      while raw_input("Enjoying the course? (y/n) ") not in ('y', 'n'):
          print("Sorry, I didn't catch that. Enter again:")
      

      你的代码做错了什么

      关于你的代码,你可以添加一些打印如下:

      choice = raw_input("Enjoying the course? (y/n) ")
      print("choice = " + choice)
      student_surveyPromptOn = True
      while student_surveyPromptOn:
          input = raw_input("Enjoying the course? (y/n) ")
          print("input = " + input)
          if choice != input:
              print("Sorry, I didn't catch that. Enter again:")
          else:
              student_surveyPromptOn = False
      

      上面打印出来:

      Enjoying the course? (y/n) y
      choice = y
      Enjoying the course? (y/n) n
      choice = y
      input = n
      Sorry, I didn't catch that. Enter again:
      Enjoying the course? (y/n) x
      choice = y
      input = x
      Sorry, I didn't catch that. Enter again:
      Enjoying the course? (y/n) 
      

      如您所见,在您的代码中出现问题的第一步,您的答案会初始化choice 的值。这就是你做错了。

      !=loop_condition 的解决方案

      如果您必须同时使用!= 运算符和loop_condition,那么您应该编码:

      student_surveyPromptOn = True
      while student_surveyPromptOn:
          choice = raw_input("Enjoying the course? (y/n) ")
          if choice != 'y' and choice != 'n':
              print("Sorry, I didn't catch that. Enter again:")
          else:
              student_surveyPromptOn = False
      

      但是,在我看来,Cyber​​ 的解决方案和我的更短的解决方案都更优雅(即更 Pythonic)。

      【讨论】:

      • 感谢您的帮助!我尝试了使用 != 和 loop_condition 的解决方案,但是当用户输入“y”或“n”时,它会再次提出问题。在有人第一次提出问题时输入“y”或“n”后,如何让循环退出?
      • 不客气。你说的是哪个代码?在有人输入'y''n' 后,我使用!=loop_condition 的解决方案不会再次提问。我想你已经尝试了一些不同的东西。如果是这样,请您编辑您的问题以添加这段代码吗?
      【解决方案3】:

      对此非常简单的解决方案是在循环开始之前初始化一些变量:

      choice=''
      
      #This means that choice is False now
      
      while not choice:
          choice=input("Enjoying the course? (y/n)")
              if choice in ("yn")
                  #any set of instructions
              else:
                  print("Sorry, I didn't catch that. Enter again: ")
                  choice=""
      

      这个while条件语句的意思是,只要choice变量为假--没有任何值就意味着choice=''--,然后继续#用循环 如果 choice 有任何值,则继续进入循环体并检查 特定输入的值,如果输入不满足要求的值 然后再次将 choice 变量重置为 False 值以继续提示用户 直到提供正确的输入

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-15
        • 2023-03-28
        • 2012-10-05
        • 1970-01-01
        • 1970-01-01
        • 2013-11-16
        • 2020-02-15
        • 1970-01-01
        相关资源
        最近更新 更多