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
|
1 Answer
Reset to default 0Do 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
版权声明:本文标题:python - I am having problems creating an input with a time limit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741314931a2371858.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
print()
statement inside of theexcept TimeoutOccurred
area, which means it will only print if the input times out. – John Gordon Commented Feb 23 at 15:24