【问题标题】:python while loop unexpected behaviorpython while循环意外行为
【发布时间】:2016-01-31 22:31:20
【问题描述】:

我对 Python 比较陌生,我不明白以下代码会产生随后的意外输出:

x = input("6 divided by 2 is")
while x != 3:
    print("Incorrect. Please try again.")
    x = input("6 divided by 2 is")
    print(x)

其输出为:

6 divided by 2 is 3
Incorrect. Please try again.
6 divided by 2 is 3
3
Incorrect. Please try again.
6 divided by 2 is 

为什么即使 x 等于 3,while 循环仍在执行?

【问题讨论】:

    标签: python loops while-loop


    【解决方案1】:

    input() 返回一个字符串,您将其与整数进行比较。这将始终返回 false。 您必须将 input() 包装在对 int() 的调用中才能进行有效比较。

    x = int(input("6 divided by 2 is"))
    while x != 3:
        print("Incorrect. Please try again.")
        x = int(input("6 divided by 2 is"))
        print(x)
    

    阅读更多关于int()here

    【讨论】:

      【解决方案2】:

      您收到此错误是因为您没有像这样解析输入:

      x = int(input("6 divided by 2 is"))
      

      如果你用那个替换你的输入器语句,它会起作用。

      【讨论】:

        【解决方案3】:

        输入法给出字符串。因此,您需要将类型转换为 int 为:

        x = int(input("6 divided by 2 is"))
        

        【讨论】:

          【解决方案4】:

          这是我对你问题的回答

          Guesses = 0
          while(Guesses < 101):
              try:
                  x = int(input("6 divided by 2 is: "))
                  if(x == 3):
                      print("Correct! 6 divide by 2 is", x)
                      break
                  else:
                      print("Incorrect. try again")
                      Guesses += 1
              except ValueError:
                  print("That is not a number. Try again.")
                  Guesses += 1
          else:
              print("Out of guesses.")
          

          我假设您希望用户使用 inputnumber,所以我将您的代码放入包含 try\except loopwhile\else looptry\except 循环确保如果用户inputs 是一个数字,则会出现一个ValueError,它会告诉他们他们inputted was not a number 是什么。 while\else loop 确保如果Guesses 限制不高于100,用户将被输入问题。此代码将确保如果用户guesses the answer3,则会提示用户they got the answer right and the loop will end;如果用户猜到除了 3(数字)之外的任何东西 answer will be incorrect 和用户 will be prompted the question again;如果user guesses a string 将被归类为ValueError,他们将收到their answer wasn't a number and that the user has to try again 的通知。

          考虑到很久以前有人问过这个问题,我假设您可能忘记了这个问题,或者您已经找到了答案,但如果没有尝试这个,请告诉我您是否喜欢这个答案。谢谢:)

          【讨论】:

            【解决方案5】:

            我现在实际上用 python 2.6 自己尝试过这个,并且确实得到了一个 int 而没有转换为 int。例如,当我尝试以下操作时:

            x = input("6 divided by 2 is")
            print "Your input is %s, %s" % (x, type(x))
            

            我得到以下信息:

            6 divided by 2 is 2
            Your input is 2, <type 'int'>
            

            这是版本问题吗?或者可能是环境问题(我使用的是 OS X)? 我得出的结论是,使用以前的建议使用 int() 应该是一个很好的做法。

            【讨论】:

            • input 在 Python 2 中自动评估输入,这就是为什么您应该始终使用 raw_input 的原因之一。在 Python 3 中,input 的作用类似于 Py2 的 raw_input
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-01-03
            • 1970-01-01
            • 1970-01-01
            • 2015-08-06
            • 1970-01-01
            • 1970-01-01
            • 2023-03-14
            相关资源
            最近更新 更多