0

I am trying to write a jump over command in Python.

The code needs to include input ('Press enter ...') command, but if the input isn't given at a certain time, it needs to jump over the input section and continue the code as if the input was given. How do I do this??

Example:

input("To start a loop press Enter:") 

for x in range 10
    print (x)

print("loop trough")
a06e
  • 18,594
  • 33
  • 93
  • 169

1 Answers1

0

How about using a daemon thread and a queue? Ugly solution I know, but should work everywhere.

from threading import Thread
from collections import deque
from time import sleep


def get_user_input(statement, queue):

    res = input(statement)
    queue.append(res)


if __name__ == '__main__':

    queue = deque()
    t = Thread(
        target=get_user_input,
        args=('Enter something: ', queue),
        daemon=True,
    )

    t.start()
    sleep(5)

    if len(queue) > 0:
        print('input received')
    else:
        print('no input received')
MaxNoe
  • 14,470
  • 3
  • 41
  • 46