Python: wait for user input, and if no input after 10 minutes, continue with program

Jonathan

I tried:

from time import sleep
while sleep(3): 
    input("Press enter to continue.") 

But it doesn't seem to work. I want the program to await user input, but if there's no user input after 10 minutes, continue with the program anyway.

This is with python 3.

falsetru

Why does the code not work?

time.sleep returns nothing; the value of the time.sleep(..) become None; while loop body is not executed.

How to solve it

If you're on Unix, you can use select.select.

import select
import sys

print('Press enter to continue.', end='', flush=True)
r, w, x = select.select([sys.stdin], [], [], 600)

Otherwise, you should use thread.

Windows specific solution that use msvcrt:

import msvcrt
import time

t0 = time.time()
while time.time() - t0 < 600:
    if msvcrt.kbhit():
        if msvcrt.getch() == '\r': # not '\n'
            break
    time.sleep(0.1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# Wait for user input then continue with code

From Dev

java Program does not wait for the user input

From Dev

Program doesnt wait to ask for user input

From Dev

Pause for user input but continue after X seconds

From Dev

Wait for user input to continue StreamReader in c# winforms

From Dev

Ending a python program with user input

From Dev

Processing user input in a python program

From Dev

Start a program after waiting for user input

From Dev

Program ends after user input is invalid

From Dev

Program won't continue after initializing input/output streams?

From Dev

TThread Wait for User Input

From Dev

Wait for user input

From Dev

Qt - What is the correct way to pause execution of a program to wait for user input?

From Dev

How to create textfield with pre text that continue appear after user input

From Dev

stop the user from entering an input after the 10th input

From Dev

Processing user input in simple Python program

From Dev

Need Python to register user input in my program

From Dev

after compiling python program, how to input arguments

From Dev

Rebooting program after input() automatically on python

From Dev

Continue execution of a Python update script without waiting for user input

From Dev

Program is hanging after input

From Dev

Program blocks after input

From Dev

Program is hanging after input

From Dev

Java user input nextLine() doesnt wait for input

From Java

C++ wait for user input

From Dev

Wait for process to finish, or user input

From Dev

Pause execution and wait for user input

From Dev

Swift WatchOS Wait for user input

From Dev

Program prints an extra row with value 10 after each input

Related Related

HotTag

Archive