Home>
while True:
    word = input ()
    print ("Not yet?")
    if word == "end":
        break


For example, if you have the above code, I think that if input () is not input, it will not proceed forever, but if time elapses without input, print ("is it still?") ​​Is executed. How can I do this?

It will be helpful if the following conditions are cleared with Plus Alpha.
・ During input, it will not be skipped even if time passes.

  • Answer # 1

    URL that I used as a reference (I omitted unnecessary parts this time)
    https://qiita.com/johejo/items/8e724110a008b27d6c1f

    import msvcrt
    import time
    DEFAULT_TIMEOUT = 30.0
    def input_timeout (prompt ='', timeout = DEFAULT_TIMEOUT):
        begin = time.monotonic ()
        end = begin + timeout
        line =''
        while time.monotonic ()<end:
            if msvcrt.kbhit ():
                line = input (prompt)
        return line


    If there is a key input even once, it will not be skipped, but if there is no key input even once, it can be skipped, so this is the solution.