0

I'm trying to figure out how to make python stop accepting input after a certain amount of time.

What I've got so far works, but won't stop the program until the user presses Enter. If it matters, I'm running this file in the Anaconda prompt.

#I've made everything short so it's simpler
def f():
    try:
        if a != "Accepted word":
            timer.cancel
            print("...")
            quit()

    except NameError:
            #This makes the code run anyway since if the user inputs nothing 
            #'a' will still be undefined and python will still be waiting for an input
            timer.cancel
            print("...")
            if a == "":
            quit()
            #Like I said, Python won't actually stop until an input is made
    else:
        #This runs fine if the user inputs the 'accepted word'
        timer.cancel
        print(...)
        quit()
timer = threading.Timer(5.0, f)
timer.start()
a = input(">")

To put it in different words, I want to make something so that Python will stop waiting for an input. If the user inputs anything before the timer ends, the program runs fine, which is great, unless the player inputs nothing. If they don't even press Enter, then the whole process halts. (I'm also an absolute beginner in Python. In fact, just learning how to make a timer and use the time.sleep function took me a couple hours)(Which is also, by the way, the extent of my knowledge)

Panda08
  • 1
  • 2
  • 1
    Possible duplicate of [Python 3 Timed Input](https://stackoverflow.com/questions/15528939/python-3-timed-input). Look at the first part ("If it is acceptable to block the main thread...") of the top-voted answer (not the accepted one). – iz_ Sep 06 '19 at 20:46
  • @iz_ That code essentially replicates what I already have. Granted, it's nicer than what I've written, but whenever the time is up I still need to press Enter to end the program. – Panda08 Sep 06 '19 at 21:02
  • For starters, `timer.cancel` is a method and should be called with `timer.cancel()`. – iz_ Sep 06 '19 at 21:50

2 Answers2

1

You could use Multi-threading for this.

from threading import Thread
import time
import os

answer = None


def ask():
    global start_time, answer
    start_time = time.time()
    answer = input("Enter a number:\n")
    time.sleep(0.001)


def timing():
    time_limit = 5
    while True:
        time_taken = time.time() - start_time
        if answer is not None:
            print(f"You took {time_taken} seconds to enter a number.")
            os._exit(1)
        if time_taken > time_limit:
            print("Time's up !!! \n"
                  f"You took {time_taken} seconds.")
            os._exit(1)
        time.sleep(0.001)


t1 = Thread(target=ask)
t2 = Thread(target=timing)
t1.start()
t2.start()
sks6174
  • 41
  • 1
  • 1
  • 5
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Богдан Опир Jun 27 '20 at 15:59
-2

just use time.sleep() The sleep method causes a python program to freeze for certain amount of time (in milliseconds). So, if you want to stop input() for a certain amount of time then you can do it this way,

from time import sleep
sleep(1000) #stops the program for 1 seconds
s = input()

sauce

  • It'll keep python from running code, but that would just delay the time until the input is processed. If I were to put "quit()" right underneath "s = input()", then the program could stay open indefinitely. In case I explained it poorly, I would like a line of code to close the program regardless of whether or not the user has input anything. – Panda08 Sep 06 '19 at 21:21