Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time-Limited Input? [duplicate]

What I would like to be able to do is ask a user a question using input. For example:

print('some scenario') prompt = input("You have 10 seconds to choose the correct answer...\n") 

and then if the time elapses print something like

print('Sorry, times up.') 

Any help pointing me in the right direction would be greatly appreciated.

like image 768
cloud311 Avatar asked Mar 20 '13 16:03

cloud311


People also ask

How do you limit input time in python?

So, you can just do this : import time b = True #declare boolean so that code can be executed only if it is still True t1 = time. time() answer = input("Question") t2 = time. time() t = t2 - t1 if t > 15: print("You have run out of time!") b = False if b == True: #Rest of code I hope this helped!

How do you skip inputs in Python?

There are many ways in which you can skip a line in python. Some methods are: if, continue, break, pass, readlines(), and slicing.

How do I use raw input in python?

The raw_input() function reads a line from input (i.e. the user) and returns a string by stripping a trailing newline. This page shows some common and useful raw_input() examples for new users. Please note that raw_input() was renamed to input() in Python version 3.


1 Answers

If it is acceptable to block the main thread when user haven't provided an answer:

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() 

Otherwise, you could use @Alex Martelli's answer (modified for Python 3) on Windows (not tested):

import msvcrt import time  class TimeoutExpired(Exception):     pass  def input_with_timeout(prompt, timeout, timer=time.monotonic):     sys.stdout.write(prompt)     sys.stdout.flush()     endtime = timer() + timeout     result = []     while timer() < endtime:         if msvcrt.kbhit():             result.append(msvcrt.getwche()) #XXX can it block on multibyte characters?             if result[-1] == '\r':                 return ''.join(result[:-1])         time.sleep(0.04) # just to yield to other processes/threads     raise TimeoutExpired 

Usage:

try:     answer = input_with_timeout(prompt, 10) except TimeoutExpired:     print('Sorry, times up') else:     print('Got %r' % answer) 

On Unix you could try:

import select import sys  def input_with_timeout(prompt, timeout):     sys.stdout.write(prompt)     sys.stdout.flush()     ready, _, _ = select.select([sys.stdin], [],[], timeout)     if ready:         return sys.stdin.readline().rstrip('\n') # expect stdin to be line-buffered     raise TimeoutExpired 

Or:

import signal  def alarm_handler(signum, frame):     raise TimeoutExpired  def input_with_timeout(prompt, timeout):     # set signal handler     signal.signal(signal.SIGALRM, alarm_handler)     signal.alarm(timeout) # produce SIGALRM in `timeout` seconds      try:         return input(prompt)     finally:         signal.alarm(0) # cancel alarm 
like image 84
jfs Avatar answered Sep 28 '22 08:09

jfs