在Python中,如果用户在固定的时间内未给出输入,则如何跳到下一个代码?

发布于 2025-02-06 14:51:38 字数 169 浏览 1 评论 0原文

我正在尝试制作一个程序,以提示用户在周期中进行输入。如果用户在固定的时间内不给出输入,则应以某种方式停止要求输入并再次提供新提示。
我已经尝试了线程的计时器函数,但是在给定时间结束后,它不会执行下一个代码,直到给出输入为止。
如何发挥这样的功能,请指导我。
是否有任何直接停止“输入()”过程的方法。

I am trying to make a program that prompt the user for input in a cycle. If user does not give input in a fixed time, it should somehow stop asking for input and again give a new prompt.
I have tried the Timer function from threading but in it after the given time is over it does not execute the next code until an input is given.
How to make such a function, please guide me.
Is there any direct way to stop the "input()" process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2025-02-13 14:51:38

我在Windows上尝试了一些MSVCRT

import msvcrt
import time

def countdown(time_sec):
    while time_sec:
        mins, secs = divmod(time_sec, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        if msvcrt.kbhit():
            print("entered ",msvcrt.getch().decode('ASCII'),"so now i will quit")
            break       
        print(timeformat, end='\r')
        time.sleep(1)
        time_sec -= 1

countdown(5)

I tried something with msvcrt on windows

import msvcrt
import time

def countdown(time_sec):
    while time_sec:
        mins, secs = divmod(time_sec, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        if msvcrt.kbhit():
            print("entered ",msvcrt.getch().decode('ASCII'),"so now i will quit")
            break       
        print(timeformat, end='\r')
        time.sleep(1)
        time_sec -= 1

countdown(5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文