Evan Tan
Evan Tan

Reputation: 1

I am having problems creating an input with a time limit

I want to make an input statement with a time limit, but it does not seem to work. when i input something, I just see three dots on a new line, and it still goes to TimeoutOccurred. I am very new to python, can anyone explain and help me fix this please?

Here is my code.

from inputimeout import inputimeout, TimeoutOccurred# <-- pip install inputimeout

try:
    x = inputimeout("input something ",3)
except TimeoutOccurred:
    x = "you failed to input in time"
    print(x)

Upvotes: 0

Views: 44

Answers (1)

no_rasora
no_rasora

Reputation: 17

Do you need to use that specific library? You can achieve similar functionality with built-in modules:

import sys, select
i, _, _ = select.select([sys.stdin], [], [], 5)
print(f"Input: {sys.stdin.readline().strip()}") if i else print("No input given")

This will wait for user input for 5 seconds, but requires you to press the enter key to confirm your input, in which case it will also end the timer early.

Upvotes: 0

Related Questions