0

Is it possible to add a time limit for user input in Python?

something like this:

while true:
     ans = input("please enter a text")
     timelimit = 5 seconds
     if timelimit = reach
     restart the code

the given code in the other solution is that if the time runs out it still asking for input and not restarting is there any way I can restart the script or loop it out after time runs out?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ArchDevOps
  • 99
  • 1
  • 8
  • 5
    Possible duplicate of [Python 3 Timed Input](https://stackoverflow.com/questions/15528939/python-3-timed-input) – roganjosh Sep 15 '18 at 11:41
  • 1
    Possible duplicate of [Keyboard input with timeout in Python](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python) – Souperman Sep 15 '18 at 11:46
  • i don't get the code to loop – ArchDevOps Sep 15 '18 at 11:46
  • [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – roganjosh Sep 15 '18 at 11:48
  • @roganjosh what I want to happen is that if the user don't input anything it will restart from the beginning – ArchDevOps Sep 15 '18 at 11:52
  • Yep, which is exactly what I linked to – roganjosh Sep 15 '18 at 11:52
  • @roganjosh import time from threading import Thread answer = None def check(): time.sleep(2) if answer != None: return print( "Too Slow") Thread(target = check).start() answer = input("Input something: ") this is the code but when it times up I want to restart the timer how can I do it? – ArchDevOps Sep 15 '18 at 12:04

0 Answers0