0

I've been trying to make an input, but that stops letting you input and moves on after x seconds.

How can I do this?

I have tried:

time.sleep: This just either makes you wait after you input, or doesn't let you input until the time is over.

Answers from similar questions unfortunately, don't work for me. Maybe the versions were off, or something else, but they all either tell the user if they were too late to input AFTER they inputted without interrupting, or they flat out didn't work, syntax errors and such.

for example:

import sys, select

print ("You have ten seconds to answer!")

i, o, e = select.select( [sys.stdin], [], [], 10 )

if (i):
  print ("You said", sys.stdin.readline().strip())
else:
  print ("You said nothing!")

This was copy pasted (I tried to change it to fit my version by adding parentheses around the 'print's), and I don't know what is going on here, but it results in:

Traceback (most recent call last):
  file location and information, line 5, in <module>
    i, o, e = select.select( [sys.stdin], [], [], 10 )
io.UnsupportedOperation: fileno
>>> 

I have also tried:

from threading import Timer

timeout = 10
t = Timer(timeout, print, ['Sorry, times up'])
t.start()
prompt = "You have %d seconds to choose the correct answer...\n" % timeout
answer = input(prompt)
t.cancel()

which results in:

(entered before 10 seconds was up)

>>>

Basically, great, moves on.

but, if I enter it after the 10 seconds:

You have 10 seconds to choose the correct answer...
asdf
Sorry, times up
>>> 

Basically, it lets me know afterwards, without interrupting, which I want it to do.

So, yeah, I have looked at other answers, but none of them have worked (There are more that I didn't include here, but they all go the way of the second, or they need modules I don't have).

I am not sure whether it's a version issue, or something else, but everybody seems satisfied with the other answers, while I am experiencing failures. If someone could provide something that works, I would be grateful. If it is an issue on my end, well, sorry for taking your time.

It should be repeatable.

Fun facts:

  1. I only have standard library, strict parents, etc

  2. Mac

  3. IDLE 3.7.3

Aidan Tung
  • 55
  • 6
  • You need another thread which is stopping the input thread. See this answer for example: https://stackoverflow.com/a/62611967/2463796 – scenox Dec 27 '20 at 23:37
  • 1
    `select` expects object with method `fileno()` and standard `sys.stdin` has `fileno()` - but if you run code in some `IDE` (or in python in interactive mode) then it may replace `sys.stdin` with own object to catch user input and this object may not have `fileno()` and you get `io.UnsupportedOperation: fileno`. So try code without interactive mode - `python script.py` – furas Dec 28 '20 at 00:37

0 Answers0