0

I am running a timer that gets input from user. However, if the user is unable to input anything, the timer should stop and the code should end but it keeps waiting for input even after time has ended. Can anyone help what I am doing wrong here? TIA

    import sys
    import threading as th
    from time import *
    
    
    def countdown():
        global my_timer



    my_timer = 4

    for x in range(my_timer):
        my_timer = my_timer - 1
        sleep(1)

    print("out of time \n")


countdown_thread = th.Thread(target=countdown)

countdown_thread.start()

while my_timer > 0:

    print('q1')
    sleep(1)
    answer = input('choose your answer\n')
    sleep(1)

    if my_timer == 0:
        sys.exit()
  • Does this answer your question? [Keyboard input with timeout?](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout) I like the `select` approach: it does not require multiple threads, signals, or counting down a number of seconds, which makes it a bit simpler. – Wander Nauta Sep 13 '22 at 09:41

0 Answers0