跳过输入函数超时 [英] Skip the input function with timeout

查看:32
本文介绍了跳过输入函数超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 3.7 编写任何程序.

I'm making any program in Python 3.7.

我想在特定时间后跳过 input 功能.

I want to skip input function after a specific time.

我的代码结构类似于下面的粗略代码.

My code has the structure like the following rough code.

def functionA():
    ...(skip)...


def functionB():
    ...(skip)...

#TIMEOUT = 0.5
while True:
    TXT = None
    TXT = input("Enter: ")

    if TXT == None:
        functionA()
    elif 'NAME' in TXT:
        functionB()
    elif TXT == 'EXIT':
        break
    else:
        pass

我想在 TIMEOUT 时间后跳过 TXT = input("Enter: ") 行,0.5 秒.我怎样才能按照我想要的方式制作这个流程的代码?

I wanna skip the line TXT = input("Enter: ") after TIMEOUT time, 0.5 sec. How can I make the code of this flow the way I want?

推荐答案

您可以使用 inputimeout 模块

您可以通过运行 cmd 并输入此命令来安装模块

You can install the module by running cmd and typing this command

pip install inputimeout

你可以这样使用

from inputimeout import inputimeout, TimeoutOccurred
try:
    var = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
    var = ''

使用步骤

  1. 导入文件中的模块
  2. 启动 try 方法
  3. 创建一个变量,而不是输入使用inputimeout 函数并输入值as prompt=timeout=
  4. except TimeoutOccurred: 输入 var 的值,如果发生超时
  1. Import the module in file
  2. start the try method
  3. make a variable and instead of input use inputimeout function and enter values as prompt= and timeout=
  4. In except TimeoutOccurred: enter the value of the var if timeout is occured

这篇关于跳过输入函数超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆