gpt4 book ai didi

python-3.x - 对输入设置时间限制

转载 作者:行者123 更新时间:2023-12-04 01:33:43 24 4
gpt4 key购买 nike

我已经搜索了一段时间,但我没有找到有效的方法我想做的是:

prompt=str(input('press "l" within two seconds:'):
if prompt=='l':
print('ta da')
else:
print('loser')

我如何为其添加时间限制?

最佳答案

如果您不关心线程安全,这是一个很好的方法:

import datetime
import threading
win = False
time_out = False
def MyThread1(t_name, prompt, time_var):
global win
global time_out
prompt=str(input('press "l" within two seconds:'))
win = False
if(not time_out and prompt == 'l'):
win = True
print('ta da')
else:
print('loser')

def MyThread2(t_name, prompt, time_var):
global win
global time_out
while (time_var > datetime.datetime.now()):
pass
time_out = True

time_var = datetime.datetime.now() + datetime.timedelta(seconds=2)
prompt = 'l'

t1 = threading.Thread(target=MyThread1, args=("Thread-1", prompt, time_var))
t2 = threading.Thread(target=MyThread2, args=("Thread-2", prompt, time_var))
t1.start()
t2.start()

关于python-3.x - 对输入设置时间限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44379292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com