【问题标题】:How to exit the program after max 3 attempts using Python, for exception program , if you dont get the desired output?对于异常程序,如果您没有获得所需的输出,如何在使用 Python 最多 3 次尝试后退出程序?
【发布时间】:2021-09-19 05:59:49
【问题描述】:

对于异常程序,如果你没有得到想要的输出,如何在使用 Python 最多 3 次尝试后退出程序?

while True:
         try:
            x = int(input("Please enter a number: "))
            break
         #except Exception as e:
         #     print (e)
         except ValueError:
             print ("You have entered the non-numeric value. Enter the numerical value.")
         except KeyboardInterrupt:
             print ("\nYou have press Ctr+C.")
             exit (1)

【问题讨论】:

  • 除了主要问题,请保持缩进标准 - 大多数人使用 4 个空格 per PEP-8,但您在不同的地方使用了 9、3 和 4。

标签: python exception


【解决方案1】:
nothing = 0
while nothing < 3:
    nothing += 1
    try:
       x = int(input("Please enter a number: "))
       break
         #except Exception as e:
         #     print (e)
    except ValueError:
        print ("You have entered the non-numeric value. Enter the numerical value.")
    except KeyboardInterrupt:
        print ("\nYou have press Ctr+C.")
        break

【讨论】:

    【解决方案2】:

    您想在 3 次尝试后退出。试试这个并计算输入是错误的:

    num_err = 0
    while num_err < 3:
        try:
            x = int(input("Please enter a number: "))
        except ValueError:
            print ("You have entered the non-numeric value. Enter the numerical value.")
            num_err += 1
        except KeyboardInterrupt:
            print ("\nYou have press Ctr+C.")
            break
    

    【讨论】:

      【解决方案3】:

      试试:

      c = 0
      while c < 3:
           c += 1
           try:
              x = int(input("Please enter a number: "))
              break
           except ValueError:
               print ("You have entered the non-numeric value. Enter the numerical value.")
           except KeyboardInterrupt:
               print ("\nYou have press Ctr+C.")
               break
      

      【讨论】:

        【解决方案4】:

        使用sys.exit() 停止整个脚本

        import sys
        
        while True:
             try:
                x = int(input("Please enter a number: "))
             #except Exception as e:
             #     print (e)
             except ValueError:
                 print ("You have entered the non-numeric value. Enter the numerical value.")
             except KeyboardInterrupt:
                 print ("\nYou have press Ctr+C.")
                 sys.exit()
        

        【讨论】:

          【解决方案5】:

          我阅读了上面提交的所有代码,但缺少一件事,即如果用户 enter 两个错误值然后enter 一个正确值之后他只有一次机会。也就是说,如果用户enter 在输入两个正确的input 后出现任何错误的input,则循环将中断。

          所以我试图解决这个问题。看我的代码...

          count = 0
          while True:
              try:
                  x = int(input("Please enter a number: "))
                  count = 0
                  break
              except ValueError:
                  count += 1
                  print("You have entered the non-numeric value.Only three invalid inputs are allowed. Enter the numerical value.")
          
              except KeyboardInterrupt:
                  print("\nYou have press Ctr+C.")
                  exit(1)
              if count == 3:
                  break
          

          我试图解释我在code 中所写的内容。 正如您所提到的,一个用户只能enter 三个invalid input 所以我增加了variable count 每次userenterinvalid input。但如果userenter 变为有效的input,则count 将是0。这又意味着user 可以enter 最多三个invalid input。在entering 三个invalid input 之后,count 将是3loop break

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-11-10
            • 1970-01-01
            • 1970-01-01
            • 2015-09-04
            • 2021-05-07
            • 1970-01-01
            • 2023-03-24
            • 2023-04-10
            相关资源
            最近更新 更多