admin管理员组

文章数量:1287627

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)

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)
Share Improve this question asked Feb 23 at 14:44 Evan TanEvan Tan 11 bronze badge 4
  • Put the print outside the except – Cincinnatus Commented Feb 23 at 14:47
  • If you move print(x) to the left, it works fine in my testing. – Subir Chowdhury Commented Feb 23 at 14:53
  • You have put the print() statement inside of the except TimeoutOccurred area, which means it will only print if the input times out. – John Gordon Commented Feb 23 at 15:24
  • 1 I just see three dots on a new line I don't see anything in this code that would print three dots. How exactly are you running the code? – John Gordon Commented Feb 23 at 15:53
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: pythonI am having problems creating an input with a time limitStack Overflow